MATLAB Answers

Extract multiple matrices from an array by excluding specified numbers.

2 ビュー (過去 30 日間)
Görkem Bam
Görkem Bam 2021 年 5 月 22 日
コメント済み: Görkem Bam 2021 年 5 月 22 日
Let's say i got an array like [0 0 0 3 5 7 2 1 4 0 0 0 4 7 8 5 2 4] .
I want to build matrices excluding all zeroes and get 3x2 matrices with remaining values like
A = 3 2 & B= 4 5
5 1 7 2
7 4 8 4
How can this be done? Thanks.

採用された回答

David Fletcher
David Fletcher 2021 年 5 月 22 日
編集済み: David Fletcher 2021 年 5 月 22 日
Will do the job in this case, but is not massively robust. Would need additional code to enforce the number of elements in the vector being reshaped if there is a chance it will not be a multiple of (six in this case)
vec=[0 0 0 3 5 7 2 1 4 0 0 0 4 7 8 5 2 4];
%Remove zeros
vec(vec==0)=[];
index=1;
for iter=1:6:numel(vec)
%Reshape remaining vector into a 3x2 and store
mat(:,:,index)=reshape(vec(iter:iter+5),[],2);
index=index+1;
end
mat(:,:,1)
ans = 3×2
3 2 5 1 7 4
mat(:,:,2)
ans = 3×2
4 5 7 2 8 4
  1 件のコメント
Görkem Bam
Görkem Bam 2021 年 5 月 22 日
Thanks for your quick reply

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

その他の回答 (0 件)

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by