Can I change iteration value in following for loop?

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!

 Accepted Answer

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 Comments

Hi @Jan, thank you so much for the response! However, this was not what I wanted. I understand, and I'm sorry it is hard to identify the desired output. I will try to explain a bit more.
if R_vx is as follows;
R_vx = (1:3:142).';
I'd like the output velx to be like,
velx(:,:,1,1) =
1 1
2 4
3 7
4 10
5 13
6 16
7 19
8 22
9 25
10 28
11 31
12 34
velx(:,:,2,1) =
1 37
2 40
3 43
4 46
5 49
6 52
7 55
8 58
9 61
10 64
11 67
12 70
and so on.
nf1 = 3; nf2 = 4;
R_vx = (1:3:142).';
panel_no = 1:12;
A = reshape(R_vx, 12, []);
B = repmat(panel_no.', 1, size(A, 2));
C = permute(cat(3, B, A), [1,3,2]);
size(C)
ans = 1×3
12 2 4
C(:, :, 1)
ans = 12×2
1 1 2 4 3 7 4 10 5 13 6 16 7 19 8 22 9 25 10 28
C(:, :, 2)
ans = 12×2
1 37 2 40 3 43 4 46 5 49 6 52 7 55 8 58 9 61 10 64
Okay, but where does the 4th dimension come from? Do you want to repeat this array 3 times along the 4th dimension?
C = repmat(C, 1, 1, 1, nf1)
Thank you, @Jan. However, it's not what I was looking for.
The code I provided explained what happens, I think. Although I cannot think of a rational way to implement it in order to get the desired output. To repaeat a little more,
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
You can see that the 2nd dimension of the matrix velx has the same 12 elements in every iteration. Obviously, because iii here does not change. What I wanted was to have the first 12 elements of R_vx in first "set" (velx(:,:,1,1)) and the second 12 elements in the second "set" (velx(:,:,2,1)) and so on. Do I make sense?
Further, I have posted this earlier and might provide more context. I apologize if the descriptions are vague, but I really appreciate your feedback and responses.
@James: "Although I cannot think of a rational way to implement it in order to get the desired output." - As far as I understand this means, that the shown code does not do, what you want. Posting the code repeatedly does not explain anything, if it does not work.
"have the first 12 elements of R_vx in first "set" (velx(:,:,1,1)) and the second 12 elements in the second "set" (velx(:,:,2,1))" - This is what my code does. But R_vx = (1:3:142).' contains 4 blocks of 12 elements only. Therefore my code creates the output as [12 x 2 x 4] array. Where do the elements of the 4th dimension come from?
I'm still trying to find out by questions for clarifications, what the wanted output is. Maybe you can create it manually and posting it as MAT file?
Another bold guess:
velx = zeros(length(panel_no), 2, nf2, nf1); % Pre-allocate
c = 0;
for i = 1:nf1
for ii = 1:nf2
for iii = 1 : length(panel_no)
c = c + 1;
velx(iii,:, ii, i) = [panel_no(iii), R_vx(c)];
end
end
end
Does this create the wanted output?
Yes! The bold guess works! :)
Introducing c was the missing link. Thank you so much!
@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]);

Sign in to comment.

More Answers (0)

Categories

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

Asked:

on 21 Dec 2022

Commented:

Jan
on 22 Dec 2022

Community Treasure Hunt

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

Start Hunting!