Creating a new matrix of size A with columns n:n+56 and zeros

1 visualización (últimos 30 días)
Ulrike Zijlstra
Ulrike Zijlstra el 25 de Mayo de 2020
Comentada: Ameer Hamza el 26 de Mayo de 2020
Hi everyone,
I have a 2464 by 2464 matrix A. I created matrix A1 with the first 56 columns of A using the following code:
A1=zeros(2464,2464);
A1(:,1:56)=A(:,1:56);
Does anyone know how I can create matrix A2 to A44 using a loop? where A2 would be
A2=zeros(2464,2464);
A2(:,57:112)=A(:,57:112);
etc.etc.
Thanks a lot in advance!

Respuestas (1)

Ameer Hamza
Ameer Hamza el 25 de Mayo de 2020
Editada: Ameer Hamza el 26 de Mayo de 2020
Using variable names like A2, A3, A4, ... is never a good idea: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval. Better to use cell array
A = rand(2464,2464);
A1 = repmat({zeros(2464,2464)}, 1, size(A, 2)/56);
for i=1:numel(A1)
col_range = (56*(i-1)+1):(56*i);
A1{i}(:, col_range) = A(:,col_range);
end
Access each group of 44 columns like this
A2{1}
A2{2}
..
A2{44}
  4 comentarios
Ulrike Zijlstra
Ulrike Zijlstra el 26 de Mayo de 2020
That's perfect, thank you so much.
I have a matrix for 14 different years. The actual names I'm using are d_Q01, d_Q02, ..., d_Q14. (I named my matrix A in my initial question to make the question more straightforward)
Ameer Hamza
Ameer Hamza el 26 de Mayo de 2020
Try something like this
d_Q = {d_Q01, d_Q02, d_Q03, d_Q04, d_Q05, d_Q06, d_Q07, ...
d_Q08, d_Q09, d_Q10, d_Q11, d_Q12, d_Q13, d_Q13};
A1 = cell(1, numel(d_Q));
for k=1:numel(d_Q)
A = d_Q{k};
A1{k} = repmat({zeros(2464,2464)}, 1, size(A, 2)/56);
for i=1:numel(A1{k})
col_range = (56*(i-1)+1):(56*i);
A1{k}{i}(:, col_range) = A(:,col_range);
end
end
The output A1 is a nested cell array.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by