Summation column matrix using for loop

18 ビュー (過去 30 日間)
Shiela Harkhoe
Shiela Harkhoe 2015 年 11 月 5 日
回答済み: Ursala Waqar 2018 年 10 月 10 日
Hi, I have a question. For my homework I have a 4x5 matrix and I'm supposed to use the for statement to find the sums of each of the columns.
I've figured it out using sum, but I don't quite understand the 'for' function. It is a loop function, but how does it work? I don't get that and therefore I'm not able to write the code for it.. Can someone explain this to me? I've read the description on this site but it didn't make me understand it.
My code using sum:
A=[0 1 2 3 4;5 6 7 8 9;4 3 2 1 0;5 6 7 8 9];
sum(A(:,1))
sum(A(:,2))
etc.

採用された回答

Star Strider
Star Strider 2015 年 11 月 5 日
It’s not necessary to use a loop to take the sum of the columns, because the sum function does that by default. All you need is:
Asum = sum(A);
If you want to take the sum across the rows instead, you can do that by adding the second dimension argument:
Asumr = sum(A,2);
  2 件のコメント
Star Strider
Star Strider 2015 年 11 月 5 日
Shiela Harkhoe’s ‘Answer’ became this Comment:
I know that. But for the assignment I need to use the 'for statement' and that's what I don't understand (the whole 'for' function).
Star Strider
Star Strider 2015 年 11 月 5 日
The for function creates an indexed loop that continues for the stated number of iterations, then stops (unless you tell it to stop earlier, but that’s a topic for another time).
I would construct your column summation loop this way:
for k1 = 1:size(A,2) % Tell ‘for’ To Iterate Over The Columns (Dimension 2)
Acolsum(k1) = sum(A(:,k1)); % Sum Column ‘k1’ And Store In ‘Acolsum(k1)’
end % End OF The Loop
See the documentation on for to get a complete description.

サインインしてコメントする。

その他の回答 (3 件)

Thorsten
Thorsten 2015 年 11 月 5 日
Show the number from 1 to 10
for i=1:10
i
end
Show the values of the first column of A
for i=1:size(A,1)
A(i,1)
end
Sum theses values
s = 0;
for i=1:size(A,1)
s = s + A(i,1);
end
Now you have to put this into another for loop that loops over the columns and you're done.
  1 件のコメント
Shiela Harkhoe
Shiela Harkhoe 2015 年 11 月 5 日
Perhaps a stupid question, but what does it mean when you put in
for i=1:size(A,1)
What does the 'size' means?

サインインしてコメントする。


Shiela Harkhoe
Shiela Harkhoe 2015 年 11 月 5 日
Thank you both! The final answer became this one...
[R,C]=size(A);
X=zeros(1,C);
for c=[1:C]
for r=[1:R]
X(c)=X(c)+A(r,c);
end
end
Stil working on understanding it completely, but it works :D
  1 件のコメント
Star Strider
Star Strider 2015 年 11 月 5 日
The size function can take two arguments, the array and the dimension you want it to return. So in my code, size(A,2) returns the size of the second dimension (number of columns).
Your code does everything correctly, including the preallocation. Your ‘A’ matrix is not large, so if you want to see how it works as it executes, change it temporarily to add the ‘Result’ vector:
[R,C]=size(A);
X=zeros(1,C);
for c=[1:C]
for r=[1:R]
X(c)=X(c)+A(r,c);
Result = [r c X(c) A(r,c)]
end
end

サインインしてコメントする。


Ursala Waqar
Ursala Waqar 2018 年 10 月 10 日
A=[0:4;5:9;4,3,2,1,0;5:9];
[r,c]=size(A);
vec=zeros(1,c);
sum=0;
for i=1:c
sum=0;
for j=1:r
sum=sum+A(j,i);
vec(i)=sum;
end
end
disp(vec)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by