フィルターのクリア

Can anyone help me to search for ones in the rows of a matrix and replace the remaining ones with zeros?

1 回表示 (過去 30 日間)
Hi!
I am quite new in Matlab. I have a a matrix with ones and zeros, for ex a = [1 1 0; 1 1 1; 0 1 1]. I want to get a new matrix with ones only in the first position it occured in A ROW, and replace all the other ones in THE ROW with zeros. So there should be only one number 1 in the row, or not at all. I have tried this a while and for loop combination, but it didn't seem to work. Any ideas?

採用された回答

Image Analyst
Image Analyst 2014 年 1 月 10 日
Do you mean like this:
a = [1 1 0; 1 1 1; 0 1 1]
b=zeros(size(a)); % Initialize.
for row = 1 : size(a, 1)
first1 = find(a(row,:), 1, 'first');
b(row, first1) = 1; % Set column of the first 1 in the row.
end
b % print to command window
a =
1 1 0
1 1 1
0 1 1
b =
1 0 0
1 0 0
0 1 0

その他の回答 (3 件)

Youssef  Khmou
Youssef Khmou 2014 年 1 月 10 日
Here is the general case:
A=round(rand(10));
for n=1:10
for p=1:10
if (A(n,p)==1)
A(n,p+1:end)=0;
end
end
end

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 1 月 10 日
a = [1 1 0; 1 1 1; 0 1 1]
b=zeros(size(a))
b(1,:)=a(1,:)

Mischa Kim
Mischa Kim 2014 年 1 月 10 日
>> a = [1 1 0; 1 1 1; 0 1 1]
a =
1 1 0
1 1 1
0 1 1
>> b = [a(:,1)>0 zeros(length(a(:,1)),length(a(1,:))-1)]
b =
1 0 0
1 0 0
0 0 0

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by