how to find Place on non zero values in matrix?

1 回表示 (過去 30 日間)
Tha saliem
Tha saliem 2018 年 3 月 5 日
コメント済み: Tha saliem 2018 年 3 月 5 日
hey all
i need to find places of 1's in matrix A:
A = [0,0,0,1,0,0,1,1,0;0,0,1,0,0,0,0,0,1;0,0,0,0,1,0,0,0,0]
result = {[4,7,8];[3,9];5}
please help

採用された回答

Stephen23
Stephen23 2018 年 3 月 5 日
編集済み: Stephen23 2018 年 3 月 5 日
>> A = [0,0,0,1,0,0,1,1,0;0,0,1,0,0,0,0,0,1;0,0,0,0,1,0,0,0,0];
>> [R,C] = find(A);
>> X = accumarray(R,C,[],@(v){v.'});
>> X{:}
ans =
4 7 8
ans =
3 9
ans =
5
  1 件のコメント
Tha saliem
Tha saliem 2018 年 3 月 5 日
Thankyou so much

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

その他の回答 (2 件)

javad ebrahimi
javad ebrahimi 2018 年 3 月 5 日
HI dear Tha saliem
I hope that I have been able to understand what you mean, and the following code can help you
clc;clear;close all
A = [0,0,0,1,0,0,1,1,0;0,0,1,0,0,0,0,0,1;0,0,0,0,1,0,0,0,0];
B=find(A~=0)
size=size(A)
[Row,Col]=ind2sub(size,B)
result={Col(Row==1),Col(Row==2),Col(Row==3)}
  1 件のコメント
Tha saliem
Tha saliem 2018 年 3 月 5 日
Hi.. it also solves the problem. thank you for your answer.

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


Jan
Jan 2018 年 3 月 5 日
A = [0,0,0,1,0,0,1,1,0;0,0,1,0,0,0,0,0,1;0,0,0,0,1,0,0,0,0]
result = cellfun(@find, num2cell(A, 2), 'UniformOutput', 0)
  1 件のコメント
Tha saliem
Tha saliem 2018 年 3 月 5 日
This is also a good approach.. Thank You

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by