For Loop: Help me PLZ
4 views (last 30 days)
Show older comments
I have a 96x3 matrix. Each column represents a different variable in an equation. Each row represents the set of the three variables in the equation. I need to write a for loop that can print a list of the answers of all 96 equations. If this makes sense can anyone help??
4 Comments
Walter Roberson
on 29 Nov 2020
for rowidx = 1 : size(Matrix,1)
result(rowidx) = f(Matrix(rowidx, 1), Matrix(rowidx, 2), Matrix(rowidx,3) )
end
nehemiah hofer
on 29 Nov 2020
So i get an error message here im not sure where I am going wrong. I was replacing matrix for my matrix name and it seems to have a problem with result? This is what I am putting.
txt = readmatrix("ec.txt");
for rowidx = 1 : size(txt,1)
result(rowidx) = f(txt(rowidx, 1), txt(rowidx, 2), txt(rowidx,3) )
end
Thanks again for the assistance
Answers (1)
M.Many
on 28 Nov 2020
You can just use
sum(M) % it sums the columns
Or if you absolutely need to use a for loop, you can do
sum = [];
for k = 1:size(M,1)
sum = sum+ M(k,:)
end
6 Comments
Walter Roberson
on 28 Nov 2020
See my comment: https://www.mathworks.com/matlabcentral/answers/667048-for-loop-help-me-plz#comment_1167858
Just put a for loop over it.
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!