finding the index of a particular point in a matrix.

4 views (last 30 days)
Hi everyone, I hope you can help me. let's say I have 5by5 matrix. e.g. A=
0 5 2 18 8
15 0 1 4 1
1 4 0 2 3
6 7 7 0 8
10 11 12 1 0
I want to first find the minimum value vertically, while ignoring the zero, then see if it is the maximum horizontally. for this example, A(3,2)=4 satisfy the conditions. then the final output at the end that I'd like the code to give is the row and column indexes. So for the above example, Idx=3 and Idy =2 . not to mention there could be more than one point that satisfies these conditions in the matrix.
so, to break it down: 1. find the min in columns, while ignoring the zeros. 2. find whether (min part 1) is the max in its row. 3. if yes, output is the x and y indexes e.g. [Idx, Idy].
Thanks a lot in advance
** Really guys big thank you and I am very appreciative.. I see this sentenced caused confusion " not to mention there could be more than one point that satisfies these conditions in the matrix." so, please let me clarify
what I meant is I want the could to output both points not just the 1st time. so let's say that I have 2 points in a matrix satisfy the rules. for example A(3,2) and A(5,1) just example, then [Idx,Idy] should output both. So there cannot be more than one max per row or min per column, however, another point in completely different column and row that apply the rule is a possibility and should be outputted as well. I really apologies for my confusion.
Additionally, since you are awesome people, I'll be a little greedy and add, what about if A now is a 3D matrix with 5by5by10 where this computation is required across the 3D dimension. meaning [Idy,Idy] size each is gby10. g here is # of points that satisfy the conditions.
Again Thank you so much!
  3 Comments
Andrei Bobrov
Andrei Bobrov on 18 Oct 2017
Edited: Andrei Bobrov on 18 Oct 2017
Hi Omar!
Please write result for matrix:
A = [ 0 4 2 18 8
15 0 1 4 1
4 4 0 2 3
3 4 2 0 4
10 11 12 1 0];
Omar Alamoudi
Omar Alamoudi on 18 Oct 2017
Hi Andrei,
I get your point completely :) but my matrix A will not have the same value per column/ row because each point in the column represents a connection strength value from j -> I . j here is the column and I is the row. so the outflow goes from j to I and the inflow goes from I to j. I don't bother you more with my project detail. but I assure you that each point in one column/row will have different value.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 18 Oct 2017
Edited: Stephen23 on 18 Oct 2017
One simple solution:
A = [
0 5 2 18 8
15 0 1 4 1
1 4 0 2 3
6 7 7 0 8
10 11 12 1 0];
S = size(A);
B = A + 0./+(A~=0);
% Linear indices of first min value in each column:
[~,idr] = min(B,[],1);
mni = sub2ind(S,idr,1:S(2));
% Linear indices of all max values in each row:
mxi = find(bsxfun(@eq,A,max(B,[],2)));
% Get matching linear indices, convert to row/col:
I = intersect(mni,mxi);
[row,col] = ind2sub(S,I)
giving:
row = 3
col = 2
  9 Comments
az
az on 21 Jun 2019
Hello Stephen Cobeldick,
Suppose I Have a column vector wcich has multiple maxima and minima, I just want to find the index of N number of them ( prominent maxia and minima).
How thw code will work please, I think I able to calculate minima by this code but not maxima.
And if I use something like ;findpeak' then it gives a lots of local max and min as for a noisy signal. I ant to get the prominant max & min indices of the 2nd colm of the attachment.
Thank you
Walter Roberson
Walter Roberson on 22 Jun 2019
Threshold
Minimum height difference between a peak and its neighbors, specified as the comma-separated pair consisting of 'Threshold' and a nonnegative real scalar. Use this argument to have findpeaks return only those peaks that exceed their immediate neighboring values by at least the value of 'Threshold'.
MinPeakDistance
Minimum peak separation, specified as the comma-separated pair consisting of 'MinPeakDistance' and a positive real scalar. When you specify a value for 'MinPeakDistance', the algorithm chooses the tallest peak in the signal and ignores all peaks within 'MinPeakDistance' of it. The function then repeats the procedure for the tallest remaining peak and iterates until it runs out of peaks to consider.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 18 Oct 2017
Edited: Walter Roberson on 18 Oct 2017
A = [
0 5 2 18 8
15 0 1 4 1
1 4 0 2 3
6 7 7 0 8
10 11 12 1 0];
B = A; B(A==0) = NaN;
[mB, midx] = min(B);
col_matches = all(mB.' >= B(midx,:) | isnan(B(midx,:)),2); %row vector
row_idx = midx(col_matches); %automatically column vector
col_idx = find(col_matches(:)); %force column vector

Community Treasure Hunt

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

Start Hunting!