フィルターのクリア

non zero rows per column

3 ビュー (過去 30 日間)
babis
babis 2013 年 12 月 5 日
編集済み: Alfonso Nieto-Castanon 2013 年 12 月 6 日
i have a matrix. suppose
A=[1 0 8; 0 0 2; 3 0 5; 4 8 0; 0 5 3; 6 1 3; 1 6 5; 0 7 1]
and i want to get the non zero rows per column in a new matrix. in my example that will be
B = [ 1 3 4 6 7 0 0 0; 4 5 6 7 8 0 0 0; 1 2 3 5 6 7 8 0]
( if A=(m,n) B will be B=(n,m) )
  2 件のコメント
dpb
dpb 2013 年 12 月 5 日
Use the "Code" button (or insert couple spaces in front of code lines on separate line w/ blank line between it and preceding text) to format the code to be legible.
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 12 月 5 日
Babis, can you explain?

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

採用された回答

Alfonso Nieto-Castanon
Alfonso Nieto-Castanon 2013 年 12 月 5 日
編集済み: Alfonso Nieto-Castanon 2013 年 12 月 6 日
If I understand your question correctly this should do:
[a,b]=sort(A>0,1,'descend');
B=a'.*b';
  1 件のコメント
Andrei Bobrov
Andrei Bobrov 2013 年 12 月 5 日
+1

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

その他の回答 (2 件)

dpb
dpb 2013 年 12 月 5 日
"Deadahead" solution...
B=zeros(size(A))';
for i=1:size(A,2)
ix=A(:,i)~=0;
B(i,ix)=find(A(:,i));
end
  2 件のコメント
babis
babis 2013 年 12 月 5 日
i think that this is really close to what i want, thank you
dpb
dpb 2013 年 12 月 5 日
編集済み: dpb 2013 年 12 月 5 日
It reproduces you example (w/ the exception of the extra row of zeros which I presumed was an error). If that is indeed wanted, then just augment the end result. You can, of course, with care to keep parens nested properly, do away with the intermediary I used for clarity of exposition. So what is on "really close" about it instead of "dead on"?
It should be reasonably easy to accumarray or otherwise vectorize it w/ the idea given altho it's not convenient here at the moment...

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


José-Luis
José-Luis 2013 年 12 月 5 日
your_mat = ndgrid(1:size(A,1),1:size(A,2));
your_mat(A==0) = 0;
your_mat(your_mat==0) = Inf;
your_mat = sort(your_mat);
your_mat(your_mat==Inf) = 0;

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by