【问题标题】:MATLAB how to use FIND function with IF statementMATLAB如何使用带有IF语句的FIND函数
【发布时间】:2010-06-20 17:04:17
【问题描述】:

此问题与How can I merge this data in MATLAB?有关

如何在IF 语句中使用FIND 函数?例如如果我有这些数据:

20  10  1
20  11  1
20  15  1
23  10  1
23  10  1
23  12  0

规则 1: 第 3 列的数据必须为 1。

规则 2: 如果n是第1列的当前索引,如果n等于第1列的n-1(20=20),则索引n和n-1的第2列的数据合并。

20  21  0
20  15  0
20  0   0
23  20  0
23  0   0
23  12  0

已编辑:

fid=fopen('data.txt');
A=textscan(fid,'%f%f%f');
fclose(fid);
in = cell2mat(A)'; %'# fix for SO formatting - Jonas

%# rows2mergeFrom are the rows where the second row equals the first row of col 1
%# and where the third column is 1. rows2mergeInto indicates the rows from which  the
%# values of the following rows should be added
rows2mergeFrom = find(in(2:end,1) == in(1:end-1,1) & in(2:end,3) == 1) + 1;

out = in;
out(rows2mergeFrom-1,2) = out(rows2mergeFrom-1,2) + out(rows2mergeFrom,2);

%# data that has been shifted up gets subtracted from the 'rows2mergeFrom'-rows
out(rows2mergeFrom,2) = out(rows2mergeFrom,2) - in(rows2mergeFrom,2);

%# remove the ones in the third column from any row that has been involved in a 
%# merge operation
out([rows2mergeFrom;rows2mergeFrom-1],3) = 0

fid = fopen('newData.txt','wt');  
format short g;
fprintf(fid,'%g\t %g\t %g\n',out);  %'# Write the data to the file
fclose(fid);  

【问题讨论】:

  • 结果和你描述的不符!?
  • 我认为数组应该是 26,而不是 25,在位置 (2,2)
  • 谢谢乔纳斯...但我没有得到相同的输出..
  • @Jessy:这很奇怪。当我从答案中复制粘贴并运行代码时,我确实得到了答案末尾的输出 - 与您复制的代码相同。

标签: matlab function find if-statement


【解决方案1】:

不需要 if 语句。您需要的是一个逻辑数组,它允许find 提取要合并的行的行索引。

已更新以符合新规则

in = [20  10  1
20  11  1
20  15  1
23  10  1
23  10  1
23  12  0];

%# rows2mergeFrom are the rows where the second row equals the first row of col 1
%# and where the third column is 1. rows2mergeInto indicates the rows from which  the
%# values of the following rows should be added
rows2mergeFrom = find(in(2:end,1) == in(1:end-1,1) & in(2:end,3) == 1) + 1;

out = in;
out(rows2mergeFrom-1,2) = out(rows2mergeFrom-1,2) + out(rows2mergeFrom,2);

%# data that has been shifted up gets subtracted from the 'rows2mergeFrom'-rows
out(rows2mergeFrom,2) = out(rows2mergeFrom,2) - in(rows2mergeFrom,2);

%# remove the ones in the third column from any row that has been involved in a 
%# merge operation
out([rows2mergeFrom;rows2mergeFrom-1],3) = 0

out =
20    21     0
20    15     0
20     0     0
23    20     0
23     0     0
23    12     0

【讨论】:

    【解决方案2】:

    据我所知,您不需要使用 find 或 if。你只需要逻辑索引。

    如果我正确理解您的问题,Amro 指出您的输出与您对逻辑的描述不符是正确的。试试下面的脚本:

    m = [20  10  0; ...
         20  11  0; ...
         20  15  1; ...
         23  10  1; ...
         23  10  1];
    merge = (m(1:end-1, 1) == m(2:end, 1)) & (m(2:end,3) == 1)
    mnew = m(2:end, :);
    madd = m(1:end-1,2) + m(2:end,2);
    mnew(merge, 2) = madd(merge);
    mnew = [m(1,:); mnew]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-29
      • 1970-01-01
      • 1970-01-01
      • 2019-09-20
      • 1970-01-01
      • 1970-01-01
      • 2013-05-09
      • 2014-08-08
      相关资源
      最近更新 更多