Finding value in Matrix to corresponding minvalue Position in different Matrix
9 views (last 30 days)
Show older comments
Alexander Hummel
on 27 Sep 2023
Commented: Bruno Luong
on 6 Oct 2023
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
Accepted Answer
Bruno Luong
on 27 Sep 2023
Edited: Bruno Luong
on 27 Sep 2023
A=randi(10,5)
B=randi(10,5)
[Amin,jmin]=min(A,[],2)
% 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
[Amin,Bmin]
4 Comments
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))
More Answers (0)
See Also
Categories
Find more on Resizing and Reshaping Matrices 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!