필터 지우기
필터 지우기

How to replace minimum values with 0 and maximum with 1 in an array

조회 수: 6 (최근 30일)
Vishal Sharma
Vishal Sharma 2017년 6월 19일
편집: Jan 2017년 6월 19일
I have an array of
A = [ 1 4 3 4 3 4 5 6 4 5 6 7;
2 3 4 5 3 4 5 6 2 3 4 2]
I want to replace maximum value in sets of four value with ‘1’ and remaining with ‘0’ of each row. So, as to get result as follows:--
A = [ 0 1 0 0 0 0 0 1 0 0 0 1;
0 0 0 1 0 0 0 1 0 0 1 0]

채택된 답변

Guillaume
Guillaume 2017년 6월 19일
편집: Guillaume 2017년 6월 19일
What you didn't say is that your set of four values is by row. Matlab works by column. You'll make your life much easier if you work the same way as matlab. In this particular case, it would be even easier if you stored your array in columns of 4 rows.
A = [ 1 4 3 4 3 4 5 6 4 5 6 7; 2 3 4 5 3 4 5 6 2 3 4 2]
[~, maxloc] = max(reshape(A', 4, [])); %transpose to work by column instead of row
maxloc = sub2ind([4, numel(maxloc)], maxloc, 1:numel(maxloc));
A = zeros(size(A'));
A(maxloc) = 1;
A = A' %transpose back
Much of the work above is just to get the array in the correct shape and back to the shape you have.

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by