Clear Filters
Clear Filters

Loop to Organize Matrices

3 views (last 30 days)
Anas Khan
Anas Khan on 26 Nov 2020
Edited: Ameer Hamza on 26 Nov 2020
I want to split up a large matrix (Vdata) with 768 columns of data into 12 matrices, each with 64 columns of data. I could do this by hand and assign each matrix the correct columns, but I want this to be end-user friendly and be able to be done by just running a script. I have come up with the code below thus far. But, I can't seem to figure out how to use a third looping variable to create new matrices for example (Col1 to Col12 in steps of 1: Col1, Col2, Col3,...).
n = [64:64:768];
m = n-63;
for i = m
for j = n
Col = Vdata(:,i:j);

Accepted Answer

Ameer Hamza
Ameer Hamza on 26 Nov 2020
Edited: Ameer Hamza on 26 Nov 2020
Naming variables like Col1, Col2, ... is a very bad coding practice: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval. It is much easier to use cell arrays. For example
Vdata = rand(100, 768);
Col = mat2cell(Vdata, size(Vdata, 1), 64*ones(size(Vdata, 2)/64, 1));
Col is a cell array with each element having 64 columns.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!