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

result(rowidx) = f(Matrix(rowidx, 1), Matrix(rowidx, 2), Matrix(rowidx,3) )
and sum afterwards
nehemiah hofer
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!
for rowidx = 1 : size(Matrix,1)
result(rowidx) = f(Matrix(rowidx, 1), Matrix(rowidx, 2), Matrix(rowidx,3) )
end
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
M.Many 2020년 11월 28일

0 개 추천

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
Anthony Kubik 2020년 11월 28일
Ok, this makes sense. But will this give me the sum of all the equations, or will it give me each individual sum in a list. I think i worded my question poorly, i need 96 different answers in a list so i can make a graph after. Thanks!!
Walter Roberson
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
Anthony Kubik 2020년 11월 28일
I wanted to plot the results of all 96 values, but I do not know how to get the results of all 96 using the for loop.
M.Many
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
Anthony Kubik 2020년 11월 28일
Ok, i am a college student taking my first coding class that honestly does not know what i am being asked either. But i will try and put it into better terms. I have a 96x3 matrix, each row is three variables that make an equation. I need a for loop that will do each of the 96 equations, and give me a list of 96 answers to each individual equation. From the picture, i need a list of
x=r(1,1),y=r(1,2),z=r(1,3) put into the equestion
x=r(2,1), y=r(2,2),z=r(2,3)
Where the row increases to 96. Hope this makes more sense, really pareciate the help.
I also added a picture of my matrix.

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

카테고리

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

제품

태그

질문:

2020년 11월 28일

댓글:

2020년 11월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by