How to store in a for loop

5 ビュー (過去 30 日間)
Mikkel Ibsen
Mikkel Ibsen 2017 年 5 月 11 日
コメント済み: Stephen23 2017 年 5 月 12 日
Hello
I have a data that consist of 274x64 data that is double.
I want to make a loop that saves the data per 4 columns so I don't have to write the code like this.
CPT1 = [Data(:,1) Data(:,2) Data(:,3) Data(:,4)];
CPT2 = [Data(:,5) Data(:,6) Data(:,7) Data(:,8)];
CPT3 = [Data(:,9) Data(:,10) Data(:,11) Data(:,12)];
CPT4 = [Data(:,13) Data(:,14) Data(:,15) Data(:,16)];
This is what I've been trying:
CPT = cell(16,1);
for j=0:4:columns/4-4
for i=1:4
CPT() = [Data(:,i+j) Data(:,i+1+j) Data(:,i+2+j) Data(:,i+3+j)];
end
end
But I don't know how to make i save them as 16 individual "CPT" What do I do?
Best Regards
  1 件のコメント
Stephen23
Stephen23 2017 年 5 月 12 日
Use mat2cell: this will be the simplest solution.

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

採用された回答

Walter Roberson
Walter Roberson 2017 年 5 月 11 日
ncol = size(Data,2);
ngroups = ncol/4;
CPT = cell(groups, 1);
for K = 1 : groups
CPT{K} = Data(:, (K-1)*4+[0 1 2 3]);
end
Or...
CPT = mat2cell(Data, size(Data,1), 4 * ones(1, size(Data,2)/4) );
  2 件のコメント
Mikkel Ibsen
Mikkel Ibsen 2017 年 5 月 11 日
Am I missing something?
First error: Undefined function or variable 'groups'.
ncol = size(Data,2);
ngroups = ncol/4;
CPT = cell(groups, 1); <- Is it ngroups ?
for K = 1 : groups <- Is it ngroups ?
CPT{K} = Data(:, (K-1)*4+[0 1 2 3]);
end
When I tried changing the groups into ngroups, i got the error:
Subscript indices must either be real positive integers or logicals.
Walter Roberson
Walter Roberson 2017 年 5 月 11 日
ncol = size(Data,2);
ngroups = ncol/4;
CPT = cell(ngroups, 1);
for K = 1 : ngroups
CPT{K} = Data(:, (K-1)*4+[1 2 3 4]);
end

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

その他の回答 (0 件)

カテゴリ

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