MATLAB Answers

How to divide an array into sperate vectors.

4 views (last 30 days)
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.

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 13 Oct 2020
use reshape() to transform it to 4x250 or 250x4 then loop through row or column
Fangjun Jiang
Fangjun Jiang on 13 Oct 2020
for row=1:size(NewM,1)
for column=1:size(NewM,2)

Sign in to comment.

More Answers (1)

Ameer Hamza
Ameer Hamza on 13 Oct 2020
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

Community Treasure Hunt

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

Start Hunting!