how to find the max element in each column and row and replace it with 1. If conflicting elements occur, look for the next max element.

2 ビュー (過去 30 日間)
hallo, i have this code and this code select the high value element but it just select the element with different column. help me finish the code so the element can be selected with different columns and rows. thankss
U=3;
D=3;
MX= zeros(U,D);
MB= zeros(U,D);
MP=rand(3)
for y=1:U
x=max(max(MP))
[U,D] = find(MP==x)
MP(U,:)=0
MX(U,D)=MP(U,D)
MB(U,D)=1
end
  4 件のコメント
Ameer Hamza
Ameer Hamza 2020 年 6 月 17 日
Can you explain how MPouput is created from MP?
Raja Zufar
Raja Zufar 2020 年 6 月 17 日
first, MP selected the max value of the matrix i assume [9 P(1,2)] and change it to '1'
second, MP finds the max value but cannot be the same as row and column P (2,1) and MP select [7 P(2,3) and change it to '1'
third, MP find the max valu but cannot be the same as row and column P(2,1) and P(2,3) and MP select [4 P(3,1) and change it to '1'

サインインしてコメントする。

採用された回答

Rajil Kansal
Rajil Kansal 2020 年 6 月 17 日
Hey,
I am assuming that you want to find max element in each column and row and replace them with 1. Also only a single 1 should be present in each row and column in final output, If conflicting element occurs look for next max element.
This could be achieved by this code:
MP = [8 9 1;3 5 7;4 6 2];
[row,col] = size(MP);
for i= 1: row
x = max(max(MP));
[r,c] = find(MP==x);
MP(r,:)=0;
MP(:,c)=0;
MP(r,c)=1;
end
MP
  2 件のコメント
Raja Zufar
Raja Zufar 2020 年 6 月 17 日
Thank you, my code really work now its so helpfull :)
the cyclist
the cyclist 2020 年 6 月 17 日
Please see my answer, which points out that this solution is not valid for some input matrices.
(Or possibly I misunderstood what you wanted.)

サインインしてコメントする。

その他の回答 (2 件)

the cyclist
the cyclist 2020 年 6 月 17 日
編集済み: the cyclist 2020 年 6 月 17 日
Blatantly stealing ideas from Ameer's and Rajil's solutions here. But I think there are some potential issues with those, specifically:
  • doing an in-place solution
  • assuming that the original input matrix doesn't have elements that are less than 1
For example, I don't think those solutions will work on this input:
rng default
MP = rand(4)-0.5;
I think this solves both issues mentioned above:
[row,col] = size(MP);
MP_output = zeros(row,col);
for i= 1: row
x = max(MP(:)); % If R2018b or later, could use x = max(MP,[],'all');
[r,c] = find(MP==x);
MP_output(r,c) = 1;
MP(r,:)=-Inf;
MP(:,c)=-Inf;
end
The output you want is given by the variable MP_output.
  3 件のコメント
the cyclist
the cyclist 2020 年 6 月 18 日
I'm glad the other solution is fine for you.
In general, good programming practice would be to either
  • use an algorithm that works on more general inputs, OR
  • make sure you comment your code, indicating the input requirement
That way, someone else who uses your code (or maybe just "future you") will not be confused why the algorithm doesn't work if they try on different input.
You may be thinking "I'm only going to use this code on this one small program, or homework, so I don't need to worry about this", but I think it is still worthwhile to practice these habits.
Raja Zufar
Raja Zufar 2020 年 6 月 19 日
Im really thankfull for the advice because im new at matlab code. And i dont know if its okay im asking the question and put in like 300 line main.m file and 20 function code. I think it gonna make people who answering confuse

サインインしてコメントする。


Ameer Hamza
Ameer Hamza 2020 年 6 月 17 日
編集済み: Ameer Hamza 2020 年 6 月 17 日
Try this
MP = [8 9 1;
3 5 7;
4 6 2];
MPouput = zeros(size(MP));
for i=1:size(MP,1)
[~, idx] = max(MP(i, :));
MPouput(i, idx) = 1;
MP(:, idx) = 0;
end
Result
>> MPouput
MPouput =
0 1 0
0 0 1
1 0 0

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by