필터 지우기
필터 지우기

Specifying values in 2D array when I know the indices

조회 수: 2 (최근 30일)
MichailM
MichailM 2022년 11월 30일
댓글: MichailM 2022년 11월 30일
I have the short example below. With the max function I find the indices of the maximum values whithin each column of array A. I want an array C which will include the values of array B by using the idx values I specified before. So, C will be C = [1 2 3 4 5]. How can I do that without a for-loop?
A = [1 1 1 1 100;
100 1 1 1 1
1 1 100 1 1
1 1 1 100 1;
1 100 1 1 1];
[~,idx] = max(A,[],1);
B = [1 1 1 1 5;
1 1 1 1 1
1 1 3 1 1
1 1 1 4 1;
1 2 1 1 1];

채택된 답변

Fangjun Jiang
Fangjun Jiang 2022년 11월 30일
A = [1 1 1 1 100;
100 1 1 1 1
1 1 100 1 1
1 1 1 100 1;
1 100 1 1 1];
[~,idx] = max(A,[],1,'linear')
idx = 1×5
2 10 13 19 21
B = [1 1 1 1 5;
1 1 1 1 1
1 1 3 1 1
1 1 1 4 1;
1 2 1 1 1];
C=B(idx)
C = 1×5
1 2 3 4 5

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by