Array lengths and loops.

조회 수: 3 (최근 30일)
F P
F P 2012년 11월 8일
Hi,
I have a while loop set up to add a new number to the end of two arrays (postsk and weight) with every cycle of the loop. The loop runs fine, but when I then try to plot them vs. each other, I find out that the arrays are different lengths. Any idea why this is happening? Is there a limit to the number of bits in each loop? Could I be running out of memory in one loop?
The following is my code (I have removed the maths equations carried out in the while loop as I doubt those are the reason and make the code a bit easier to read).
while (bst < 63)
(this is where the ommited equations live, but essentially just figure out values for the upcoming if loop).
if (resskin >= 1 & resstif >= 1 & reseuler >= 1 & resmat >= 1)
postsk(i) = tsk;
weight(i) = (((((tsk/1000)*(bsk/1000))+((tst/1000)*(bst/1000)))*(length/1000))*1600);
i = i + 1;
end
bst = bst + 1
end
Any help would be great! Thank you!

채택된 답변

Star Strider
Star Strider 2012년 11월 8일
The only possibility I can think of that might be causing the unequal vectors problem (not knowing more about your code) is that you previously used weight or postsk as an array, and those arrays are larger than they are assigned in the loop you posted. One cure for that might be to assign:
weight = [];
postsk = [];
before your while loop. (I assume you don't use either variable in your while loop other than in your if block.)
  댓글 수: 4
Matt Fig
Matt Fig 2012년 11월 8일
편집: Matt Fig 2012년 11월 8일
Good call, Star Strider. The mystery hiding in 'the unposted code' cracked by you!
Star Strider
Star Strider 2012년 11월 9일
편집: Star Strider 2012년 11월 9일
Thanks, Matt. I didn't realize you were answering at the same time until after I submitted my answer.

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

추가 답변 (1개)

Matt Fig
Matt Fig 2012년 11월 8일
편집: Matt Fig 2012년 11월 8일
Couple of things. It looks like you are masking both the LENGTH function and the I function with a variable. Don't do that - choose a different variable name. Second, is it possible that sometimes those mysterious variables could be returning a vector or an empty? Is one of them a cell array?
Before your call to plot, put this:
whos postsk weight
and show the output.
  댓글 수: 1
F P
F P 2012년 11월 8일
I should not be getting any empties or a vector, but I will try to fix the masking issue.
I also put in whos plotsk weight and got the following:
Name Size Bytes Class Attributes
postsk 1x123102 984816 double
weight 1x90778 726224 double
Thanks for your help and time!

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

카테고리

Help CenterFile 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