How select a first value from the binary matrix ?

I have below matrix i want to select one value from each column which will be 1 make other 0 at return i will get 10*10 matrix.Selection of 1 such that a11 from 1st column,a22 from 2 column,a13 from the third column,a14 from the 4column and so on and make other value in the column 0.If we go for the indexing is ok for the small matrix but in bigger matrix we don't know the position of the 1 value.So is it possible to changed what i want.I have 1300*500 size matrix this is only for the example.
Thanks in advanced for your help.
matrix = [
0 1 0 0 1 0 0 1 0 1 0 1 1 1 0 1 0 0 1 0
1 1 0 0 1 1 1 0 0 1 0 1 0 1 0 0 1 1 1 1
0 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1
0 1 1 1 1 1 1 0 0 0 0 1 0 1 1 0 0 0 1 0
1 1 0 0 0 1 0 0 1 1 1 1 0 0 0 0 0 0 0 1
0 0 0 0 0 1 0 0 1 0 1 1 0 1 1 1 1 1 1 0
1 0 1 0 1 0 0 1 1 0 1 1 0 0 1 0 0 1 0 1
1 1 0 0 1 0 1 0 0 0 0 0 1 1 0 1 1 1 0 1
1 0 1 0 0 1 0 0 1 0 0 0 0 1 1 0 0 0 1 0
0 0 1 0 1 0 1 1 0 0 0 1 1 0 0 0 1 1 0 1
];

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2019년 8월 20일
편집: Andrei Bobrov 2019년 8월 22일

1 개 추천

out = cumsum(cumsum(matrix)) == 1;
or
out = cumsum(matrix) == 1 & matrix;

추가 답변 (1개)

Walter Roberson
Walter Roberson 2019년 8월 19일
편집: Walter Roberson 2019년 8월 19일

1 개 추천

[~,idx] = max(matrix);
output = sparse(idx,1:size(matrix,2),1,size(matrix,1),size(matrix,2));
Note: in the case where there are no 1's in a particular column, then this code will pick the first row of that column.
[~,idx] = max(matrix);
output = sparse(idx,1:size(matrix,2),true,size(matrix,1),size(matrix,2));
is potentially more useful for your purposes.

댓글 수: 5

vikash kumar
vikash kumar 2019년 8월 19일
편집: vikash kumar 2019년 8월 19일
Thanks for helping me.What you gave the aswer is really nice and give the location of 1 value.It is possible to make the matrix of same n*n size from which i get the result such that only result value should be 1 and other will be 0.
Thank in advanced.Thank u Thank u so much for helping me .
I am not sure what difference there is between what I provided and what you want. I suspect that you are being confused by the sparse() output. If so then
[~,idx] = max(matrix);
output = full(sparse(idx,1:size(matrix,2),true,size(matrix,1),size(matrix,2)));
Got it thank u so much
darova
darova 2019년 8월 20일
You can also accept the answer. I will be pleased
With your 1300 x 500 array, only 1 in 1300 rows has a non-zero entry for each column, so sparse can be a lot more storage efficient than full(); in my test, the full version takes 41 times more memory.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2019년 8월 19일

편집:

2019년 8월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by