如何更新 matlab 数据集中行中的多个值?

How to update several values in row in matlab's dataset?

我有一个 46x46 数据集,我需要更新单行中的多个值。

例如:

data =   
A B C D  
0 0 0 0  
0 0 0 0  
0 0 0 0

我想让它像

A B C D  
0 0 0 0  
0 1 0 1  
0 0 0 0 

我可以这样写吗:

data(2, ['B', 'D']) = [1, 1];

谢谢!

是:

data(2, [2 4]) = mat2dataset([1 1]);

或者简单地说:

data(2, [2 4]) = mat2dataset(1);

NB: As a side note, you should avoid using datasets. According to the documentation: "The dataset data type might be removed in a future release. To work with heterogeneous data, use the Matlab table data type instead."

如果您有 table (which is the recommended replacement of the dataset 类型),您可以使用 'VariableNames' 属性:

来引用列的名称

首先构建table:定义列名并用零填充所有列:

data = table(zeros(3,1), zeros(3,1), zeros(3,1), zeros(3,1), ....
       'VariableNames', {'A','B','C','D'});

这给出了

data = 
    A    B    C    D
    _    _    _    _
    0    0    0    0
    0    0    0    0
    0    0    0    0

然后,要更改问题条目中的条目,您可以使用

data(2, ismember(data.Properties.VariableNames, {'B' 'D'})) = {1, 1};

data(2, ismember(data.Properties.VariableNames, {'B' 'D'})) = deal({1});

或更直接

data.B(2) = 1;
data.D(2) = 1;

以上任一项给出

data = 
    A    B    C    D
    _    _    _    _
    0    0    0    0
    0    1    0    1
    0    0    0    0