필터 지우기
필터 지우기

How to organize loop outputs

조회 수: 2 (최근 30일)
Nick
Nick 2012년 5월 24일
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
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
Nick 2012년 5월 24일
Is there a way to remove the elements after each iteration? I have no idea how to do that kind of code!

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

답변 (1개)

Honglei Chen
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
Daniel Shub
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
Nick 2012년 5월 24일
Sorry I think I confused people. I edited my post to explain.

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by