# How to select certain columns of a matrix only when the values in certain rows showing the the double of the value in a particular row of a column?

3 views (last 30 days)
Kim Arnold on 14 Aug 2020
Edited: Shae Morgan on 14 Aug 2020
Hi everybody!
I have a Matrix with 5 rows and 2300 columns M(5,2300).
0.2 0.3 0.4 2 ...
3 0.5 1.9 2.5 ...
2 0.7 0.2 3 ...
1 1 1 1 ...
0.5 0.1 1.5 3.1 ...
looks like above. The numbers you see are ratios because all the values of a particular column were divided by the value in the 4th row (which is now 1). I intend to just keep the columns for future analysis when at least one value of the other rows in a column (row1:3 and row5 values) are >=2, meaning that they should be at least the double of the value in the 4th row. As an output from the columns above it should give me an index like [1,0,0,1].
Could you probably suggest a nice solution? I'm thankful for every help.

KSSV on 14 Aug 2020
Let A be your 5*n matrix.
% GEt ratio
R = A(1:3,:)./A(5,:) ;
idx = R>=2 ; % get indices where ratio is >= 2

Kim Arnold on 14 Aug 2020
Dear KSSV,
Thanks for your answer but is not really what I'm intending to do.
So, the above shown values are already calculated ratios, there's no need to calculate the ratio again. I just mentioned that the shown values are ratios ( I divided all the values of a particular column by the value in the 4th row, this is why I have always 1 in the columns of the 4th row).
By using just idx= R>=2 I get a Matrix like
0 0 0 1
1 0 0 1
1 0 0 1
0 0 0 0
0 0 0 1
as I mentioned I would like to have a result as [1,0,0,1,....] telling me for the 2300 columns where my condition is true or false. one value of the other rows (than row 4) in a column (row1:3 and row5 values) are >=2, meaning that they should be at least the double of the value in the 4th row.
Of course I can now take the idx with zeros and ones and count the number of ones in a column and when I have at least one 1 I will keep the column for future analysis. But I think there should be a simpler way combining this two steps.
KSSV on 14 Aug 2020
So you have a matrix of 0 and 1. How about using?
val = sum(idx) ;
id = find(val) ;
Kim Arnold on 14 Aug 2020
yes, this works. thanks. probably it is better to do it in 2 steps.
Thanks!

Bruno Luong on 14 Aug 2020
Edited: Bruno Luong on 14 Aug 2020
Mkeep = M(:,any(M>=2,1))

Show 1 older comment
Bruno Luong on 14 Aug 2020
I editted out the bug 10 minutes ago
Kim Arnold on 14 Aug 2020
Yeah with your code i get the columns with their values but I just wanted to have it with idx 1,0 ...
anyways thanks as well for your help!
Bruno Luong on 14 Aug 2020
shouldn't call |1 0 0 1] an indexes, they form a logical array.
any(M>=2,1)
give you that.

Shae Morgan on 14 Aug 2020
Edited: Shae Morgan on 14 Aug 2020
do this in two steps: 1) find the indices where the values > 2 and then 2) sum the columns - if all rows 1-3 and 5 are less than 2, then this sum will be 0. If one of them has a value >=2, then the sum will be >0.
idx= M>=2;
out= sum(idx)>0
out will give you an index of 0's and 1's for each column. if 1, then it's something you should analyze later (i.e., has a value 2x row 4)

Kim Arnold on 14 Aug 2020
Dear Shae,
thanks, is what KSSV also recommended to do. Just was looking for like one line of code :).
Thanks as well for your input.
Shae Morgan on 14 Aug 2020
single line answer by combining the variables :) Thanks for the consideration! Should work for KSSV's answer too:
out= sum(M>=2)>0 %my implementation, single line
id = find(sum(M>=2)) ; %KSSV's solution, single line