How to organize loop outputs
이전 댓글 표시
Hi. I have made a loop, but my problem is that the outputs that are given are all different sizes, so I am unable to put these into a matrix.
What i have right now
for w = 1:(size(e(2))
u = find (x>=v(w)&x<=e(w))
end
my output looks like this
u =
4 5 10
u =
6
u =
3 9
u =
1 2 7 8
As you can see I cannot just assign u(w) because a matrix can't be made.
I can give the rest of the script if necessary. But I am just looking for a way to organize these better, so that I can continue using all of these different outputs. Not just the last one of u.
EDIT: Hhhmmm, there seems to be some confusion over the answer given.
My problem is that u{4} = 1 2 3 4 5 6 7 8 9 10 Unless I did it wrong, I keep getting this.
This is not what I am trying to get.
I am trying to get some way of storing all the outputs from this loop, separately.
So something like assigning variables such that u(1) = 4 5 10
u(2) = 6
u(3) = 3 9
u(4) = 1 2 7 8
or a Matrix such as:
4 5 10 0
6 0 0 0
3 9 0 0
1 2 7 8
Sorry for the confusion in those comments!
댓글 수: 2
Honglei Chen
2012년 5월 24일
Looks like you are removing the found elements from x after each iteration? But this is not in your posted code. The answer I gave is just a framework you can use so that you can access the result with u{1}, u{2}, and so on. If there are extra steps you need to perform within each iteration, you still need to do that. And from what you describe here, it seems that the order of iteration may matter.
Nick
2012년 5월 24일
답변 (1개)
Honglei Chen
2012년 5월 24일
You could consider using cell
for w = (size(e(2)):-1:1
u{w} = find (x>=v(w)&x<=e(w))
end
I also reversed the iteration order to preallocate.
댓글 수: 7
Nick
2012년 5월 24일
Oleg Komarov
2012년 5월 24일
Honglei's solution should store all results. Please check again.
Nick
2012년 5월 24일
Oleg Komarov
2012년 5월 24일
u{1}, u{2}, u{3} ...
Nick
2012년 5월 24일
Daniel Shub
2012년 5월 24일
What do you mean by it doesn't work? If the ith iteration results in nothing being found, then the ith element of y will be empty.
Nick
2012년 5월 24일
카테고리
도움말 센터 및 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!