Note that there are some explanatory texts on larger screens.

plurals
  1. POMATLAB how to use FIND function with IF statement
    text
    copied!<p>This question is related to <a href="https://stackoverflow.com/questions/3071558/matlab-how-to-merge-data">How can I merge this data in MATLAB?</a></p> <p>how to use the <code>FIND</code> function with <code>IF</code> statement? e.g. if I have this data:</p> <pre><code>20 10 1 20 11 1 20 15 1 23 10 1 23 10 1 23 12 0 </code></pre> <p>Rule 1: Data of column 3 must be 1.</p> <p>Rule 2: If <code>n</code> is the current index of column 1, if <code>n</code> equal <code>n-1</code> (20=20) of column 1, data of column 2 of index n and n-1 <strong>is merged.</strong> </p> <pre><code>20 21 0 20 15 0 20 0 0 23 20 0 23 0 0 23 12 0 </code></pre> <p><strong>EDITED:</strong> </p> <pre><code>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) &amp; 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); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload