필터 지우기
필터 지우기

one-by-one matrix assignment

조회 수: 2 (최근 30일)
Antonio
Antonio 2018년 2월 6일
댓글: Antonio 2018년 2월 6일
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일
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일
[~,idx] = min(A);
s = size(A);
A(sub2ind(s,idx,1:s(2))) = B(end,:);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by