How to divide an array into sperate vectors.

2 ビュー (過去 30 日間)
Mohannad Alzard
Mohannad Alzard 2020 年 10 月 13 日
コメント済み: Fangjun Jiang 2020 年 10 月 13 日
i have a 1X1000 array, i want to use the first 4 elements in each iteration in a for loop, so the first iteration first 4, second iteration the next 4 elements and so on.

採用された回答

Fangjun Jiang
Fangjun Jiang 2020 年 10 月 13 日
use reshape() to transform it to 4x250 or 250x4 then loop through row or column
  2 件のコメント
Mohannad Alzard
Mohannad Alzard 2020 年 10 月 13 日
Thank you, but do you have any idea how to loop over the first 4 elements too ?, so first loop is to loop over the rows and the second loop is to loop over the first 4 elements of each row.
Thank you.
Fangjun Jiang
Fangjun Jiang 2020 年 10 月 13 日
M=1:1000;
NewM=reshape(M,[],4);
for row=1:size(NewM,1)
for column=1:size(NewM,2)
NewM(row,column)
end
end

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

その他の回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 10 月 13 日
Another approach is to convert it to a cell array
M; % 1x1000;
M_parts = mat2cell(M, 1, 4*ones(250,1));
for i = 1:numel(M_parts)
x = M_parts{i}; % x will be 1x4 vector.
% process 'x' variable
end
  1 件のコメント
Mohannad Alzard
Mohannad Alzard 2020 年 10 月 13 日
Thank you so much!

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by