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

4 ビュー (過去 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 Center および File ExchangeOperators and Elementary Operations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by