Problem Using Nested For Loops

8 vues (au cours des 30 derniers jours)
Scott Banks
Scott Banks le 6 Nov 2024
Commenté : Scott Banks le 7 Nov 2024
Dear all,
I have the following problem. I want to manipulate the following matrix:
X = [1 2 3 4 5;
6 7 8 9 10;
11 12 13 14 15;
16 17 18 19 20]
X = 4×5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
From here I want to separate the columns in this matrix and add values to each number. Thus, for example I want to take:
X(:,1)
ans = 4×1
1 6 11 16
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
And add some values to it. I want to do this for all columns. For X(:,2), X(:,3), X(:,4) and X(:,5)
I have tried this using a nested for loop, but it is not correct.
for i = 1:5
for j = 1:4
if j == 1 || j == 3
X(:,i) = X(:,j) - 10
else j == 2 || j == 4
X(:,i) = X(:,j) + 20
end
end
end
X = 4×5
-9 2 3 4 5 -4 7 8 9 10 1 12 13 14 15 6 17 18 19 20
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = logical
1
X = 4×5
22 2 3 4 5 27 7 8 9 10 32 12 13 14 15 37 17 18 19 20
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
X = 4×5
-7 2 3 4 5 -2 7 8 9 10 3 12 13 14 15 8 17 18 19 20
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = logical
1
X = 4×5
24 2 3 4 5 29 7 8 9 10 34 12 13 14 15 39 17 18 19 20
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
X = 4×5
24 14 3 4 5 29 19 8 9 10 34 24 13 14 15 39 29 18 19 20
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = logical
1
X = 4×5
24 34 3 4 5 29 39 8 9 10 34 44 13 14 15 39 49 18 19 20
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
X = 4×5
24 -7 3 4 5 29 -2 8 9 10 34 3 13 14 15 39 8 18 19 20
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = logical
1
X = 4×5
24 24 3 4 5 29 29 8 9 10 34 34 13 14 15 39 39 18 19 20
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
X = 4×5
24 24 14 4 5 29 29 19 9 10 34 34 24 14 15 39 39 29 19 20
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = logical
1
X = 4×5
24 24 44 4 5 29 29 49 9 10 34 34 54 14 15 39 39 59 19 20
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
X = 4×5
24 24 34 4 5 29 29 39 9 10 34 34 44 14 15 39 39 49 19 20
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = logical
1
X = 4×5
24 24 24 4 5 29 29 29 9 10 34 34 34 14 15 39 39 39 19 20
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
X = 4×5
24 24 24 14 5 29 29 29 19 10 34 34 34 24 15 39 39 39 29 20
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = logical
1
X = 4×5
24 24 24 44 5 29 29 29 49 10 34 34 34 54 15 39 39 39 59 20
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
X = 4×5
24 24 24 14 5 29 29 29 19 10 34 34 34 24 15 39 39 39 29 20
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = logical
1
X = 4×5
24 24 24 34 5 29 29 29 39 10 34 34 34 44 15 39 39 39 49 20
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
X = 4×5
24 24 24 34 14 29 29 29 39 19 34 34 34 44 24 39 39 39 49 29
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = logical
1
X = 4×5
24 24 24 34 44 29 29 29 39 49 34 34 34 44 54 39 39 39 49 59
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
X = 4×5
24 24 24 34 14 29 29 29 39 19 34 34 34 44 24 39 39 39 49 29
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = logical
1
X = 4×5
24 24 24 34 54 29 29 29 39 59 34 34 34 44 64 39 39 39 49 69
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
So I aiming to get the 1st and 3rd element in each column of the X matrix and subtract 10 from it. Likewise, I am aiming to take the 2nd and 4th elements in the X matrix in each column and add 20 to them.
Thus I should get finally:
X = [-9 -8 -7 -6 -5;
26 27 28 29 30;
1 2 3 4 5;
36 37 38 39 20]
X = 4×5
-9 -8 -7 -6 -5 26 27 28 29 30 1 2 3 4 5 36 37 38 39 20
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
I am actually trying to solve a much bigger set of data, but I have used this example because it falls on the same principle.
I am not very good with nested for loops. So, can someone help please?
Many thanks
  1 commentaire
Stephen23
Stephen23 le 6 Nov 2024
"I am actually trying to solve a much bigger set of data, but I have used this example because it falls on the same principle."
Avoid loops, just add them.
"I am not very good with nested for loops."
Why do you need to use loops?

Connectez-vous pour commenter.

Réponse acceptée

Shivam Gothi
Shivam Gothi le 6 Nov 2024
Modifié(e) : Shivam Gothi le 6 Nov 2024
Upon investigating your code, it seems that there is an error with the expression coded inside the "if-else" decision block. I rectified it and got the following solution, which aligns with your desired solution.
X = [1 2 3 4 5;6 7 8 9 10;11 12 13 14 15;16 17 18 19 20];
for j = 1:5
for i = 1:4
if (i == 1 || i == 3)
X(i,j) = X(i,j) - 10;
elseif (i == 2 || i == 4)
X(i,j) = X(i,j) + 20;
end
end
end
disp(X)
-9 -8 -7 -6 -5 26 27 28 29 30 1 2 3 4 5 36 37 38 39 40
Also, you can do the operation with single for loop also as shown below:
X = [1 2 3 4 5;6 7 8 9 10;11 12 13 14 15;16 17 18 19 20];
for i = 1:4
if (i == 1 || i == 3)
X(i,:) = X(i,:) - 10;
elseif (i == 2 || i == 4)
X(i,:) = X(i,:) + 20;
end
end
disp(X)
-9 -8 -7 -6 -5 26 27 28 29 30 1 2 3 4 5 36 37 38 39 40
I hope it helps !
  2 commentaires
Scott Banks
Scott Banks le 6 Nov 2024
Thanks, Shivam Gothi
Voss
Voss le 6 Nov 2024
There is no need to use loops for this.

Connectez-vous pour commenter.

Plus de réponses (1)

Voss
Voss le 6 Nov 2024
No loops required:
X = [1 2 3 4 5;
6 7 8 9 10;
11 12 13 14 15;
16 17 18 19 20]
X = 4×5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
V = [-10; 20; -10; 20]
V = 4×1
-10 20 -10 20
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
X = X + V
X = 4×5
-9 -8 -7 -6 -5 26 27 28 29 30 1 2 3 4 5 36 37 38 39 40
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
For your actual data set, you'd have to construct the appropriate column vector V. Indexing operations or the repmat or repelem functions may be convenient for that. It's hard to say without knowing what V needs to be for your real data.
e.g.,
V = repmat([-10; 20],size(X,1)/2,1) % assumes X has an even number of rows
V = 4×1
-10 20 -10 20
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
or
V = zeros(size(X,1),1);
V(1:2:end) = -10;
V(2:2:end) = 20
V = 4×1
-10 20 -10 20
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  1 commentaire
Scott Banks
Scott Banks le 7 Nov 2024
Yes, thanks, Voss. That is much, much more simple.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by