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 个评论

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.
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.
First, thanks for the reply. a1 and a2 are only a constant value while u and x are 2D array. row by n. column by t. in this case it will be 5x1000. adj2 is 5x5 matrix.
I am getting an error message "Index in position 2 exceeds array bounds (must not exceed 1)." hmm maybe the problem like Bob said. "x(j) - x(i) will return a reduced vector, while x(i,t) - x(i,t-1) will return a single element" any advice what should i do to make it be the same size?

请先登录,再进行评论。

 采纳的回答

Sriram Tadavarty
Sriram Tadavarty 2020-3-17
编辑:Sriram Tadavarty 2020-3-17

0 个投票

Hi Nur,
If the code is updated to run from time t set from 2 to 1000, this should go away with the error you see.
The subtraction of x(j) and x(i) would only do it for the first five elements all the time. I think this is not the intention. It should be x(j,t) and x(i,t)
With these changes the code looks as such:
for t=2:1000
for i=1:5
for j=1:5
u(i,j,t)= a1*adj2(i,j)*(x(j,t)- x(i,t))+a2*(x(i,t)-x(i,t-1));
end
end
u=sum(u,2)
end
u = permute(u,[1 3 2]);
Hope this helps.
Regards,
Sriram

6 个评论

Hi Sriram, ive tried the code but the error message still show up :(
Hi Nur, I do see the outputs for the code. Did you change the variable t?
yes, i did. what is the output did you get? is it u(1:5,1:1000)?
Should the output be of size 5 x 1000, then you need to even use the third variable t.
Like,
u(i,j,t)= a1*adj2(i,j)*(x(j,t)- x(i,t))+a2*(x(i,t)-x(i,t-1));
% At the end of all the loops
u = permute(u,[1 3 2]);
Let me know what is intended in u?
problem solve Sriram. Thank you very much!
That's nice to hear Nur. Do accept the answer if it helped.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心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!

Translated by