Finding value in Matrix to corresponding minvalue Position in different Matrix

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 Kommentare

"I am quite new to Matlab"
You posted a MATLAB question in 2017. How come you are still new to MATLAB six year later?
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!!

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

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 Kommentare

Many thanks! :)
Unfortunately, I can not use sub2ind because I am using R2021b.
Do you know any alternative way??
:)
sub2ind was introduced before R2006a. That's not a reason to not use it.
"P.S.: sub2ind is not working, I use R2021b. :("
What exactly "not working" means in this context?
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))
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by