logical indexing from matrix to vector
4 ビュー (過去 30 日間)
古いコメントを表示
How to extract nonzero columns from rows (in a matrix whose each rows have only one column as 1 and others 0) and put them into a vector rows?
A=rand(4,5);
B=rand(4,5);
C=rand(4,5);
D=logical([1 0 0 0 0; 0 0 0 0 1; 0 1 0 0 0; 0 0 1 0 0]);% each row has 1 nonzero column
E=zeros(4,3);
%get first column of E, E(:,1) to be all rows of A at the nonzero column of A. For instance,
% F=zeros(4,5), then F(D)=A(D) will put those nonzero columns of A into matching positions in F. But I want those to go to E(:,1).
% Then, E(:,2) contains all elements B(D), and E(:,3) contains all elements
% C(D).
0 件のコメント
採用された回答
Voss
2021 年 12 月 21 日
Here is one way:
A=rand(4,5);
B=rand(4,5);
C=rand(4,5);
display(A);
display(B);
display(C);
D=logical([1 0 0 0 0; 0 0 0 0 1; 0 1 0 0 0; 0 0 1 0 0]);% each row has 1 nonzero column
display(D);
E=zeros(4,3);
[r,c] = find(D);
for i = 1:numel(r)
E(r(i),:) = [A(r(i),c(i)) B(r(i),c(i)) C(r(i),c(i))];
end
display(E);
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Resizing and Reshaping Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!