How to copy every 500 rows to a new variable?

2 ビュー (過去 30 日間)
akshay raj
akshay raj 2015 年 11 月 16 日
回答済み: Walter Roberson 2019 年 6 月 19 日
Hi,
I have a [30000 x 57] matrix, I want to copy every new 500 rows to a new variable?
Thanks.

採用された回答

Stephen23
Stephen23 2015 年 11 月 16 日
編集済み: Stephen23 2019 年 6 月 19 日
  1 件のコメント
Ilham Hardy
Ilham Hardy 2015 年 11 月 16 日
Wow, thanks for the concise explanation.
Now I am really convinced not to dynamically named variables.
Thanks Stephen :)

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

その他の回答 (4 件)

Star Strider
Star Strider 2015 年 11 月 16 日
編集済み: Star Strider 2015 年 11 月 16 日
Another approach:
M = randi(9, 30000, 57); % Create Data
Mr = reshape(M, 50, [], 57); % Reshape
Mr = permute(Mr, [1 3 2]); % Shuffle Dimensions
Mc = mat2cell(Mr, ones(size(Mr,1),1), size(Mr,2), size(Mr,3)); % Create Cell Array
Every element of ‘Mc’, from ‘Mc{1}’ to ‘Mc{50}’ is a different matrix variable. To convert them back into double arrays for calculations, use the cell2mat function.
  2 件のコメント
Bish Erbas
Bish Erbas 2015 年 11 月 16 日
Awesome!
Star Strider
Star Strider 2015 年 11 月 16 日
Thank you!
My code does the same essential task yours does, just differently.
That brings to focus the observation that neither you nor I created 50 ‘new’ variables with our code, but instead created a single cell array with segments of the original matrix that are easier to access and work with, as ‘akshay raj’ essentially requested.
It would seem that reading and understanding the existing Answers would avoid much of the consternation that is too frequently apparent otherwise.

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


Thorsten
Thorsten 2015 年 11 月 16 日
編集済み: Thorsten 2015 年 11 月 16 日
Why do you want to create 60 new variables and double the data? That's not a good idea. You can keep your original data and just address the i'th set of your 500 rows using
ind = [1:500] + (i-1)*500;
your_function(X(ind,:));

Bish Erbas
Bish Erbas 2015 年 11 月 16 日
Do you really have to create individual variables for each 500 rows? This approach is not recommended because it is not using the array processing power of MATLAB. The preferred method is to store related data in a single array or a cell array. For example:
A = rand(30000,57);
N = (size(A,1)/500);
cellVar = cell(1,N);
for k = 1:N
cellVar{k} = A(500*(k-1)+1:500*(k-1)+500,:);
end

Walter Roberson
Walter Roberson 2019 年 6 月 19 日
result = squeeze(cellfun(@transpose, num2cell(reshape(YourMatrix.', size(YourMatrix,2), 500, []), [1 2]), 'uniform', 0));
This will give you a 60 x 1 cell array, each entry of which is a 500 x 57 array.

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by