Appending a row vector into a matrix based on given conditions

In my current code I have created a 604x1 matrix called b where I subtracted -60 and -550, the smaller of the values would then call a row in a larger matrix, 604x3, called data and append the row into one of the two empty matrices I have set up plane550 and plane60 depending on whichever value was greater than the other. Could someone guide me as to how to fix the problem in my code given below as running it gives me a single matrix that is equal to data in size.
xzplane60 = [];
xzplane550 = [];
for ii = 1:size(equakemat,1)
if abs(b(ii)-60) > abs(b(ii)-550)
xzplane550 = [xzplane550; equakemat(ii,:)];
else
xzplane60 = [xzplane60; equakemat(ii,:)];
end
end

 Accepted Answer

x = abs(b-60) > abs(b-550); % fixed typo
xzplane550 = equakemat(x,:);
xzplane60 = equakemat(~x,:);
If all the data ends up in only one matrix, then all the data is closer to one of the comparison values.

5 Comments

I dont think that gave me what I needed, I think I may have phrased the question in a bad way let me rephrase. So in my for loop I am using a 604x1 matrix called b and running it through an if else if statement where it calls the row ii from b and checks through an inequality, if it satisfies that inequality it would then call upon the row ii in matrix 604x3 called equakemat that would then be inserted into an empty matrix where after I would run a 3d plane regression on.
Oops, typo. Replace b(ii) with b.
On a 2D graph this is what my data would look like on a XZ plane, with the code you gave me all the data went to one matrix. In this graph though however, some points are plotted closer to y=550 than y=60 in which I should get 2 different matrices. How would I go about doing that as well as using ii to pull out the iith row in equakemat based off the comparison operators?
Examine your b vector closely. What exactly is in it? Pause the code in the debugger and take a look at it to make sure it is what you think it is.
ohhhh i see i used the wrong vector i had another one labelled c that gave the correct output sorry for the inconvienience.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!