How to operate > and > to extract a matrix?

Dear all, I have a matrix and i need to extract a smaller matrix based on a condition, this condition took the following shape as Mr Walter once advised me, xab and xbc and xcd are from different columns, but when i run it i found that the new matrix Qua1 took a value that is larger than 6 ! how can i fix this please.
mask1 = xab(:,1)<6 & xbc(:,1)<6 & xcd(:,1)<6;
Qua1=M(mask1, :);

 Accepted Answer

Image Analyst
Image Analyst on 18 Nov 2017
Edited: Image Analyst on 18 Nov 2017
You are getting a mask for this situation
  1. column 1 of xab is less than 6, AND
  2. column 1 of xbc is less than 6, AND
  3. column 1 of xcd is less than 6
In other words, where all 3 columns of those 3 different arrays are less than 6.
mask1 could be false if any of the x matrices were more than 6 but not all 3, for example 2 were 5 but one of them was 7. Qua1 should not take that row however since mask1 would be false there for that row.
Then you are taking rows where that is the case from M. However, the values of M can be ANYTHING and have no relation to what the values of the x matrices are. The values in M could be up in the millions.
If you want to clip Qua1 to 6, then you can do this:
Qua1(Qua1>=6) = 6;

7 Comments

First thank you for the answer, as a matter of fact, the three columns are not from different arrays, they are from the original array which is called M. I need to apply the condition above on each row of M, based on the three values from the three columns, they must be less than 6 each. if yes, then take this row and store it in Qua1. it supposed to work, but when i opened Qua1 i found the three columns include numbers more than 6 which is wrong. is it clear now?
I mean is the syntax right or wrong to write:
mask1 = xab(:,1)<6 & xbc(:,1)<6 & xcd(:,1)<6;
No, it's not. You said "as a matter of fact, the three columns are not from different arrays, they are from the original array which is called M." However, you wrote
mask1 = xab(:,1)<6 & xbc(:,1)<6 & xcd(:,1)<6;
with xab, xbc, and xcd, which are clearly NOT M like you wanted. Thus you should have written
mask1 = M(:,1) < 6;
Then, to extract only rows where that is true, you'd do
Qua1 = M(mask1, :); % Extracts ALL columns
which will extract all columns from only those rows where column 1 of M is less than 6.
Now, since Qua1 extracts all columns from M, some numbers in columns 2 and to the right may have numbers more than 6 in them, but numbers in column 1 will be guaranteed to be less than 6.
If you want Qua1 to be only column 1 of M, then do this:
Qua1 = M(mask1, 1); % Extract only column 1.
Does that answer and explain everything?
yes it explains the concept, now I am out of my office, when I am back I will try what you advised, thank you very much for your help, thank you
OK I just tried it and still gives error, now to explain more, M is my matrix which is 15 columns, three of those columns contain distances, I want to write a statement saying the following:
mask1 = 0.1*3.28084*M(:,8) < 6 & 0.1*3.28084*M(:,10) < 6 & 0.1*3.28084*M(:,12) < 6;
Qua1 = M(mask1, :); % Extracts ALL columns
how can i apply the three conditions at the same time please
They ARE being applied at the same time. Let's make the 3 conditions more explicit so you can see that:
condition1 = 0.1*3.28084*M(:,8) < 6
condition2 = 0.1*3.28084*M(:,10) < 6
condition3 = 0.1*3.28084*M(:,12) < 6;
% Find out which rows meet ALL 3 conditions at the same time.
all3Conditions = condition1 & condition2 & condition3;
% Extracts ALL columns in M but only
% in those rows meeting all 3 conditions.
Qua1 = M(all3Conditions , :);
you are more than amazing, I will give it a try, thank you very much

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!