Clear Filters
Clear Filters

one-by-one matrix assignment

2 views (last 30 days)
Antonio
Antonio on 6 Feb 2018
Commented: Antonio on 6 Feb 2018
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 Comments
Jos (10584)
Jos (10584) on 6 Feb 2018
ok. then Andrei's answer suffices.
Antonio
Antonio on 6 Feb 2018
@Jos (10584) great, thanks!

Sign in to comment.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 6 Feb 2018
Edited: Andrei Bobrov on 6 Feb 2018
[~,idx] = min(A);
s = size(A);
A(sub2ind(s,idx,1:s(2))) = B(end,:);

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!