How to organize loop outputs
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Honglei Chen
el 24 de Mayo de 2012
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.
Respuestas (1)
Honglei Chen
el 24 de Mayo de 2012
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 comentarios
Daniel Shub
el 24 de Mayo de 2012
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.
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!