I need to find the minimum values in each column of matrix "A", and then replace those min values with the values in last row of matrix "B" (which has same number of columns). Like I have these:
>> A = randi(10,10,5)
A =
3 5 9 5 8
7 6 4 10 2
8 4 1 7 4
4 7 2 8 2
7 5 8 7 5
3 7 10 10 1
5 7 8 5 7
8 3 8 2 3
6 10 2 1 10
3 7 6 7 2
>> B = randi(100,3,5)
B =
10 34 66 18 62
99 95 49 54 81
52 1 52 9 95
>> [M,I] = min(A)
M =
3 3 1 1 1
I =
1 8 3 9 6
And I want to replace the values of "M" with "B(end,:), so that:
A(1,1) = B(end,1);
A(8,2) = B(end,2);
A(3,3) = B(end,3);
A(9,4) = B(end,4);
A(6,5) = B(end,5);
I try "A(I) = B(end,:)" and "A(I(1,:)) = B(end,:)" but they do not work! Any ideas how I could do that? My real matrices are huge (1200x100000) so no way to do it by hand!

4 Kommentare

Jos (10584)
Jos (10584) am 6 Feb. 2018
So, only the last line of B is relevant?
Antonio
Antonio am 6 Feb. 2018
@Jos(10584) Not really, I'm just providing a simple example! In reality I have a loop in which mateix B updates each time and I have to call a certain row of it. But that's fine, I know how to do that part, I just need to know how to do that sort of one-by-one assignment that I asked!
Jos (10584)
Jos (10584) am 6 Feb. 2018
ok. then Andrei's answer suffices.
Antonio
Antonio am 6 Feb. 2018
@Jos (10584) great, thanks!

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 6 Feb. 2018
Bearbeitet: Andrei Bobrov am 6 Feb. 2018

3 Stimmen

[~,idx] = min(A);
s = size(A);
A(sub2ind(s,idx,1:s(2))) = B(end,:);

Weitere Antworten (0)

Kategorien

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by