Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 858301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:31:35+00:00 2026-05-15T08:31:35+00:00

This question is related to How can I merge this data in MATLAB? how

  • 0

This question is related to How can I merge this data in MATLAB?

how to use the FIND function with IF statement? e.g. 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.

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.

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);  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T08:31:35+00:00Added an answer on May 15, 2026 at 8:31 am

    There is no need for an if-statement. What you need is a logical array that allows find to extract the row indices of the rows that are to be merged.

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is related to my problem in understanding rebase, branch and merge ,
This question is related to this post but I don't see how I can
This question follows a suggestion made by @sharptooth in this related question . Can
This question is related to (but perhaps not quite the same as): Does Django
This question is related to another question which I asked yesterday! List all links
This question is related to a previous post of mine Here . Basically, I
[This question is related to but not the same as this one .] My
This question is related to this one , though I think I was a
[This question is related to but not the same as this one .] If
This question is related to the question posted here: Why isn't my custom WCF

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.