how to create a series of changes over the loop variable and can assign a value to it

how to create a series of changes over the loop variable and can assign a value to it Example
for r=1:3
A|r| = r
B|r| = A|r+1| + A|r|
end
the result
A1=1
A2=2
A3=3
B1=3
B2=5
...

 Respuesta aceptada

try :
n=1:30;
A=n;
for r=1:length(n)-1
B(r)=A(r+1)+A(r);
end
plot(A), hold on, plot(B)

3 comentarios

Thanks so much, but I my question can explain follows: I have a matrix [3x4]( rand(3,4), I create A0 variable and assign A0=rand(3,4). I want to create A1 = rand(3,4+1), A2=rand(3,4+2)...Ai=rand(3, 4+i).
for i=1:4
Ai = rand(3,4+i)
end
Please help me
hi, In this case, the dimension grows inside the loop so you need to use the cell :
for i=1:4
A{i}=rand(3,4+i);
end
Hi I have tried
for i=1:4
A{i}=rand(3,4+i);
end but have an error: "Cell contents assignment to a non-cell array object" What can I do?

Iniciar sesión para comentar.

Más respuestas (2)

You don't have a good condition for when r = the last value, for example, what is A(4) when r only goes to 3. But ignoring that last element, this will work for you:
n=10
A=1:n
B = A + [A(2:end) 0]
In command window:
n =
10
A =
1 2 3 4 5 6 7 8 9 10
B =
3 5 7 9 11 13 15 17 19 10
If you want to add the last element of A then turn the 0 into A(n).
This works just fine (if that's what you want to do):
clc;
for i=1:4
A{i} = rand(3,4+i)
end
whos A
A =
[3x5 double]
A =
[3x5 double] [3x6 double]
A =
[3x5 double] [3x6 double] [3x7 double]
A =
[3x5 double] [3x6 double] [3x7 double] [3x8 double]
Name Size Bytes Class Attributes
A 1x4 1072 cell
See how it grows with each iteration and the size of the last-added cell is bigger each time? Just what you wanted, right?

3 comentarios

Thanks you a lot
I really want to make the variables gradually increase after each iteration to assign to the created matrix after each iteration. then use these variables in other calculations
example:
A{0} = rand(3,4);
for i=1:4
A{i}= rand(3.4+i);
B{i}= A{i}*A{i+1}'
end
and the result i want:
A1 = [3x5]
A2 = [3x6]
B1 is a matric A1*A2'
....
You can't multiply a 3x5 by a 6x3 - it doesn't work. Anyway, I see you've already accepted another answer so I guess you got it solved that way.
I'm sorry. B{i} = A{i}'*{A{i+1} [5x3]*[3x6}
Now i can understand some things. Thanks you a lot.

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by