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

Jos (10584)
Jos (10584) 2018년 2월 6일
So, only the last line of B is relevant?
Antonio
Antonio 2018년 2월 6일
@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) 2018년 2월 6일
ok. then Andrei's answer suffices.
Antonio
Antonio 2018년 2월 6일
@Jos (10584) great, thanks!

댓글을 달려면 로그인하십시오.

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2018년 2월 6일
편집: Andrei Bobrov 2018년 2월 6일

3 개 추천

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

질문:

2018년 2월 6일

댓글:

2018년 2월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by