For Loop: Help me PLZ
显示 更早的评论
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 个评论
Walter Roberson
2020-11-28
result(rowidx) = f(Matrix(rowidx, 1), Matrix(rowidx, 2), Matrix(rowidx,3) )
and sum afterwards
nehemiah hofer
2020-11-29
Im not sure what you mean by just put a for loop over this could you explain? I get what a for loop is but im not sure how you would format it in this context. Thanks!
Walter Roberson
2020-11-29
for rowidx = 1 : size(Matrix,1)
result(rowidx) = f(Matrix(rowidx, 1), Matrix(rowidx, 2), Matrix(rowidx,3) )
end
nehemiah hofer
2020-11-29
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
回答(1 个)
M.Many
2020-11-28
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 个评论
Anthony Kubik
2020-11-28
Walter Roberson
2020-11-28
When you invoke the function on one set of three variable values, does it produce a vector that you want to sum() to produce an individual value that you want to record, and then later plot those values?
Or when you talked about the "sum" of all of the equations, did you just mean that you wanted to plot the results for all 96 values?
Anthony Kubik
2020-11-28
M.Many
2020-11-28
Do you want to sum the columns or the rows of the matrix ? Do you have the values for the variables, in that case it is a simple matrix multiplication? What exactly do you want ?
Anthony Kubik
2020-11-28
Walter Roberson
2020-11-28
See my comment: https://www.mathworks.com/matlabcentral/answers/667048-for-loop-help-me-plz#comment_1167858
Just put a for loop over it.
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

