Finding value in Matrix to corresponding minvalue Position in different Matrix

9 views (last 30 days)
Dear all,
I am quite new to Matlab and have the following problem:
I do have 2 Matrices A and B with the same size.
I would like to loop over each row of Matrix A to find the Minimum-value and its Column-Position.
Each resulting Column-position should be taken and looped over Matrix B to find the corresponding column-value.
Afterwards, I should receive a matrix in which I will find the values in following order:
minvalue Matrix A row1
corresponding value at position of minvalue row1 of Matrix A
minvalue Matrix A row2
corresponding value at position of minvalue row2 of Matrix A
and so on....
Afterwards, I would like to normalize the resulting Matrix to ensure, I have all values in a range in between -1 and 1
Can anybody please help me out with this?
I thought about this piece of code, but I do not manage to loop over the second Matrix with the minidx1-values to receive the value of the defined value from Matrix A in Matrix B.
for k1=1:size(A,1)
[minval1,minidx1] = min(A(k1,:));
[minval2] = B(:,minidx1:minidx1);
Result{k1}=[minval1,minval2]
end
A=cell2mat(Result)
A=transpose(A)
A=normalize(A)
P.S.: sub2ind is not working, I use R2021b. :(
Many thanks in advance to all those Matlab-cracks out there :)
  3 Comments
Alexander Hummel
Alexander Hummel on 6 Oct 2023
Hi Bruno,
One more question:
Do you know any possibility to do the same code, but instead of taking one min-value, of Matrix A I would like to take two values (max-peaks each row) and find the corresponding values in Matrix B each row.
I thought about findpeaks, but this is not proper working- I think it is dedicated to work only with one value.
many thanks!!

Sign in to comment.

Accepted Answer

Bruno Luong
Bruno Luong on 27 Sep 2023
Edited: Bruno Luong on 27 Sep 2023
A=randi(10,5)
A = 5×5
4 6 5 1 10 3 7 3 5 10 4 1 9 3 3 10 5 2 10 1 2 5 7 7 2
B=randi(10,5)
B = 5×5
7 1 9 6 5 7 7 8 2 7 4 9 5 10 10 3 10 2 6 7 5 6 1 1 5
[Amin,jmin]=min(A,[],2)
Amin = 5×1
1 3 1 1 2
jmin = 5×1
4 1 2 5 1
% Amin is A(sub2ind(size(A),(1:size(A,1))',jmin))
% Bmin = B(sub2ind(size(B),(1:size(B,1))',jmin))
m = size(B,1);
Bmin = B((1:m)'+ m*(jmin-1)) % EDIT
Bmin = 5×1
6 7 9 7 5
[Amin,Bmin]
ans = 5×2
1 6 3 7 1 9 1 7 2 5
  4 Comments
Bruno Luong
Bruno Luong on 27 Sep 2023
sub2ind is as old as the earth. You either corrupt or shadow the stock function sub2ind. Otherwise replace with
m = size(B,1);
Bmin = B((1:m)'+ m*(jmin-1))
Alexander Hummel
Alexander Hummel on 27 Sep 2023
Hi Bruno,
many thanks. I just read in the documentation that sub2ind is supported after 2021b.
But I was mistaken. I think I mixed it up.
As you mentioned:
Introduced before R2006a
Your code works fine.
MANY THANKS

Sign in to comment.

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!