How select a first value from the binary matrix ?

3 ビュー (過去 30 日間)
vikash kumar
vikash kumar 2019 年 8 月 19 日
編集済み: Andrei Bobrov 2019 年 8 月 22 日
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
];
  1 件のコメント
Selva Karna
Selva Karna 2019 年 8 月 20 日
Your first value=matrix(1,1);

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

採用された回答

Andrei Bobrov
Andrei Bobrov 2019 年 8 月 20 日
編集済み: Andrei Bobrov 2019 年 8 月 22 日
out = cumsum(cumsum(matrix)) == 1;
or
out = cumsum(matrix) == 1 & matrix;

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2019 年 8 月 19 日
編集済み: Walter Roberson 2019 年 8 月 19 日
[~,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 件のコメント
darova
darova 2019 年 8 月 20 日
You can also accept the answer. I will be pleased
Walter Roberson
Walter Roberson 2019 年 8 月 20 日
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.

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by