简体   繁体   English

MATLAB如何在IF语句中使用FIND函数

[英]MATLAB how to use FIND function with IF statement

This question is related to How can I merge this data in MATLAB? 这个问题与我如何在MATLAB中合并这些数据有关?

how to use the FIND function with IF statement? 如何在IF语句中使用FIND函数? eg if I have this data: 例如,如果我有此数据:

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

Rule 1: Data of column 3 must be 1. 规则1:第3列的数据必须为1。

Rule 2: If n is the current index of column 1, if n equal n-1 (20=20) of column 1, data of column 2 of index n and n-1 is merged. 规则2:如果n是第1列的当前索引,则如果n等于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

EDITED: 编辑:

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);  

There is no need for an if-statement. 无需if语句。 What you need is a logical array that allows find to extract the row indices of the rows that are to be merged. 您需要一个逻辑数组,该数组允许find提取要合并的行的行索引。

Updated to conform with the new rules 更新以符合新规则

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

As far as I can tell, you don't need to use find or if. 据我所知,您不需要使用find或if。 You only need logical indexing. 您只需要逻辑索引。

If I understand your question correctly, Amro is right in pointing out that your output doesn't match your description of the logic. 如果我正确理解了您的问题,Amro会指出您的输出与您对逻辑的描述不匹配是正确的。 Try the following script: 尝试以下脚本:

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM