My code is as follows:
clearvars;
zeros(1,10); %Create a row vector with 10 zeros
for i=1:1:10
A = [i.^2];
C = zeros(1,10) + A;
end
disp(C)
100 100 100 100 100 100 100 100 100 100
Now, what I originally wanted to do was add the square of each iteration on the respective i-th column of the zeros(1,10) vector, instead I get i = 10 for every position and I don't understand why it doesn't do it for each iteration but only for the last one.

댓글 수: 3

Torsten
Torsten 2022년 10월 25일
편집: Torsten 2022년 10월 25일
What are you trying to do ? Add the squares of the numbers from 1 to 10 ?
x = 1:10;
s = sum(x.^2)
s = 385
Or do you want
x = 1:10;
s = cumsum(x.^2)
s = 1×10
1 5 14 30 55 91 140 204 285 385
Giuliano
Giuliano 2022년 10월 25일
I‘m trying to add e.g 1^2 as the first column of my zero vector The final result should be 1 4 9 25 …. 100
x = (1:10).^2
x = 1×10
1 4 9 16 25 36 49 64 81 100

댓글을 달려면 로그인하십시오.

 채택된 답변

Star Strider
Star Strider 2022년 10월 25일

0 개 추천

It is necessary to index into ‘C’, however I do not know what you want as the result.
% clearvars;
C = zeros(1,10); %Create a row vector with 10 zeros
for i=1:1:10
A = [i.^2];
C(i) = A;
end
disp(C)
1 4 9 16 25 36 49 64 81 100
.

댓글 수: 3

Giuliano
Giuliano 2022년 10월 25일
편집: Giuliano 2022년 10월 25일
That‘s exactly what I wanted! Now if you don’t mind explaining, why exactly does my code not work? Thanks!
Matt J
Matt J 2022년 10월 25일
@Giuliano if so, you should Accept-click the answer.
Star Strider
Star Strider 2022년 10월 25일
@Giuliano — As always, my pleasure!
The problem is that you did not index ‘C’, so every iteration overwrote the previous iteration.
I also assigned the zeros call before the loop to ‘C’ since your obvious intent was to pre-allocate it. That should always be done if at all possible, so that is good programming practice there.
@Matt JThank you!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jim Riggs
Jim Riggs 2022년 10월 25일
편집: Jim Riggs 2022년 10월 25일

0 개 추천

Inside the loop
C = zeros(1,10) + A;
the function "zeros(1,10)" is assigning zeros to the entire C vector, then adding A.
Also, the command before the for loop "zeros(1,10)" does not save the 10 element vector.
I think what you intended is something like:
C = zeros(1,10); %Create a row vector with 10 zeros
for i=1:1:10
A = [i.^2];
C(i) = A;
end

댓글 수: 3

Giuliano
Giuliano 2022년 10월 25일
Thank you, that‘s exactly it. But why exactly does indexing the C change it so much?
Jim Riggs
Jim Riggs 2022년 10월 25일
편집: Jim Riggs 2022년 10월 25일
indexing instructs the program to put the value of "A" in the 'ith" position in the C array.
So inside the for loop, when i=1, A is computed as 1^2 = 1, and put in location C(1).
On the next ittereation of the loop, i=2, A=2^2 = 4 and is stored in position 2 in the C vector (i.e. C(2) ).
When i=3, A=3^2 = 9 and is stored in C(3), etc.
So, the way you had the loop coded, C was assigned a value of zeros(1,10), or ten zeros, then it added A to the vector, which adds A to all 10 elements of C.
Each itteration of the loop assigned ten zeros to C, then added A, so only the last itteration retained any values, and they were 10 copies of A.
so in the first itteration of the loop, i=1, A=1 and C = 1 1 1 1 1 1 1 1 1 1
In the second itteration of the loop, i=2, A=4, and C = 4 4 4 4 4 4 4 4 4 4,
etc.
Giuliano
Giuliano 2022년 10월 25일
Thanks for the detailed explanation! Honestly I thought what would happen if I somehow included i in C was that I‘d get ten vectors C that have i^2 as the i-th element and the rest zeros and that I‘d have to somehow add these then vectors then. Might be stupid, but I just started with MatLab (rather any kind of programming) last week so let me be excused! Thanks again.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2022b

질문:

2022년 10월 25일

댓글:

2022년 10월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by