How to turn a large array into multiple smaller arrays

12 ビュー (過去 30 日間)
Lori
Lori 2012 年 8 月 12 日
コメント済み: k 2020 年 7 月 17 日
Hi,
I have an array that has 31 rows and 1518 columns. I'm trying to figure out a quick way to turn that array into 759 arrays that are 31 rows and 2 columns. I want to take the first two columns, then the next two columns, etc. I was wondering this there was a way to do this in a loop, but was unsure if you could create and name new arrays in a loop. Any help would be much appreciated!
Thanks,
Lori
  1 件のコメント
Oleg Komarov
Oleg Komarov 2012 年 8 月 13 日
Please, do not post duplicate questions. Also, do not create incremental variables by splitting your array. It's a design suicide and bad programming practice.

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

回答 (3 件)

Wayne King
Wayne King 2012 年 8 月 12 日
編集済み: Wayne King 2012 年 8 月 12 日
I think you want to store the separate matrices in a cell array rather than having 759 separate matrices, right? I mean you can always just access each of the 31x2 matrices from the cell array
X = randn(31,1518);
k = 1;
for nn = 1:2:1518-1
Y{k} = X(:,nn:nn+1);
k = k+1;
end
Now you can access each of the 31x2 matrices as
Y{1}, Y{2}, etc.
  1 件のコメント
k
k 2020 年 7 月 17 日
Hi! Is there a way to add to this loop a way for the program to plot every Y{} matrix, potentially in the same layout via tiledlayout()?

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


Azzi Abdelmalek
Azzi Abdelmalek 2012 年 8 月 12 日
編集済み: Azzi Abdelmalek 2012 年 8 月 13 日
A=rand(31,1518); %example
B=reshape(A,31,2,1518/2)
for k=1: 1518/2
out.(sprintf('A%d',k))=B(:,:,k)
end
the first array is
out.A1,
thee second is
out.A2 ,
the last
out.A759

Andrei Bobrov
Andrei Bobrov 2012 年 8 月 13 日
out1 = reshape(yourmatrix,size(yourmatrix,1),2,[]);
or
out2 = mat2cell(yourmatrix,size(yourmatrix,1),2*ones(size(yourmatrix,2)/2,1));

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by