如何有条件地检索元素?

时间:2015-01-15 20:43:27

标签: matlab

我不是Matlab用户,但现在必须处理它。我无法找到如何:

[row,col]=matrix1[matrix1==100];

如何排除row = 5?

2 个答案:

答案 0 :(得分:1)

以下程序应说明您需要采取的步骤(即使可能采用更有效的解决方案):

%// Generate some numbers:
matrix1 = randi(4,5);
%// Find the indices of the elements you are looking for:
VALUE_TO_FIND = 3;
[row,col] = find(matrix1==VALUE_TO_FIND);
%// To exclude a certain row, simply "delete" the elements pointing there:
ROW_TO_IGNORE = 4;
col(row==ROW_TO_IGNORE)=[]; %//it is important that this line comes first
row(row==ROW_TO_IGNORE)=[];

如果要忽略某一列,请切换最后两行的顺序(并相应地更改括号中的条件)。

答案 1 :(得分:0)

您可以将其作为后期处理:

sel = row~= 5;
row = row(sel);
col = col(sell);