Row-by-row analysis after establishing a threshold value
Show older comments
Hi community,
I have a matrix of 3 columns X a lot of rows. My goal is to do an analysis of each row depending on a general threshold value. Values contained in cells ranged from 0 to 1, and I want to find in which of these 3 columns is the higher value, with the condition that the higher value must be over the threshold value. The analysis I plan to do need to test several possible threshold values.
However, there are a couple of situations that may occur:
1) All values are under the threshold value: In this case I need a zero as output. 2) There is more than one value over the threshold value: I need a zero as output as well.
For example, given a threshold value of 0.5, applied to this matrix:
[0.55 0.45 0.55;0.65 0.75 0.85;0.35 0.95 0.45;0.85 0.15 0.25;0.35 0.25 0.15;0.45 0.45 0.35]
The output expected is:
[0;0;2;1;0;0]
In the first two rows there is a "multiple assignment" situation (there is more than one value over the threshold value), then a 0 is the final assignment. Rows 3 and 4 have only one value over threshold value, and they are located in columns 2 and 1, respectively. Finally, rows 5 and 6 contain all values under threshold value, in this manner a 0 must be assigned (this would be an "unassigned" situation).
Thank you very much for your collaboration!!!!
Accepted Answer
More Answers (1)
A=[0.55 0.45 0.55;0.65 0.75 0.85;0.35 0.95 0.45;0.85 0.15 0.25;0.35 0.25 0.15;0.45 0.45 0.35];
T=0.5;
iUnOv=all(A<T,2) | sum(A>T,2)>1; % those that aren't to consider in max()
A(iUnOv,:)=0; % clear them
[~,B(~iUnOv)]=max(A(~iUnOv,:),[],2); % find max location for the others
>> B
B =
0
0
2
1
0
0
Look up "logical addressing"...
NB: The above excludes ==T(hreshold) from both tests. Include the '=' in the appropriate test condition if is to be inclusive on either side.
2 Comments
Diego Gómez
on 4 Sep 2018
dpb
on 4 Sep 2018
Oh, I recast two variables into one when I posted the Answer from initial code at command line and didn't follow thru completely, my bad.
I fixed the LH side, but not the RH side. What was ix is iUnOv now. Corrected the Answer, too...
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!