Borrar filtros
Borrar filtros

Can I change iteration value in following for loop?

2 visualizaciones (últimos 30 días)
Jake
Jake el 21 de Dic. de 2022
Comentada: Jan el 22 de Dic. de 2022
Suppose I have the following code.
nf1 = 3; nf2 = 4;
panel_no = 1:12;
R_vx = rand(144,1);
for i = 1:nf1
for ii = 1:nf2
for iii = 1 : length(panel_no)
velx(iii,:, ii, i) = [panel_no(iii), R_vx(iii)];
end
end
end
velx
My goal is to create a 4D double matrix, and velx provides the right structure. However, it only includes the first 12 values of R_vx (for obvious reasons, of course). If the second iteration of the for loop starts from 13 for R_vx, and the third one from 25 and so on, I'd get the desired matrix. Is there a way to implement this? TIA!

Respuesta aceptada

Jan
Jan el 21 de Dic. de 2022
Maybe you mean:
nf1 = 3; nf2 = 4;
panel_no = 1:12;
R_vx = rand(144,1);
velx = repmat([panel_no.', reshape(R_vx, 12, 12)], 1, 1, nf1, nf2);
It is hard to guess the wanted output based on a not working code.
  6 comentarios
Jake
Jake el 22 de Dic. de 2022
Yes! The bold guess works! :)
Introducing c was the missing link. Thank you so much!
Jan
Jan el 22 de Dic. de 2022
@James: Fine. Then without a loop:
nf1 = 3; nf2 = 4;
panel_no = 1:12;
R_vx = rand(144,1);
A = [repmat(panel_no, 1, numel(R_vx)/numel(panel_no)); R_vx.'];
B = reshape(A, 2, 12, 4, 3);
C = permute(B, [2, 1, 3, 4]);

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Schedule Model Components 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