combining 3 for loops
Ältere Kommentare anzeigen
Hi, All!
I have 3 "for" loops but i confuse when combining one and another. I need vector u, depend on x(j)-x(i) and x(i,t)-x(i,t-1)
this is the best function i've tried before. So glad to know that anyone can correct and help me :)
for t=1:1000
for i=1:5
for j=1:5
u(i,j)= a1*adj2(i,j)*(x(j)- x(i))+a2*(x(i,t)-x(i,t-1));
end
end
u=sum(u,2)
end
3 Kommentare
tmarske
am 16 Mär. 2020
Please use auto-indentation when nesting multiple loops like this - it makes things much easier to read.
What are the sizes of a1, adj2, a2 and x?
You are indexing the array 'x' in two different ways on the same line:
(x(j)- x(i))
and
(x(i,t)-x(i,t-1))
Both of these will return output if x is a 2-d array, but I suspect it's not what you meant to do.
Bob Thompson
am 16 Mär. 2020
Also, what exactly is the problem you're experiencing? Are you getting an error message, or are you just not getting the output you would expect. If the output is different, what does it look like, and how were you expecting it to be different?
To add to tmarske's point about the different forms of indexing, if x is a 2D array then x(j) - x(i) will return a reduced vector, while x(i,t) - x(i,t-1) will return a single element. This is not inherently a problem, depending on the size of a1, adj2, and a2, but MATLAB will not let you perform a '+' operation on two components of different sizes like that.
Nur Amalina
am 17 Mär. 2020
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!