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 comentarios

Honglei Chen
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.
Nick
Nick el 24 de Mayo de 2012
Is there a way to remove the elements after each iteration? I have no idea how to do that kind of code!

Iniciar sesión para comentar.

Respuestas (1)

Honglei Chen
Honglei Chen el 24 de Mayo de 2012

1 voto

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

Nick
Nick el 24 de Mayo de 2012
That only gives me the cell of the final value of u.
I would like to somehow continue use the data of
u = 4 5 10
u = 6
u = 3 9
as well. Maybe assign them all to different variables somehow?
Oleg Komarov
Oleg Komarov el 24 de Mayo de 2012
Honglei's solution should store all results. Please check again.
Nick
Nick el 24 de Mayo de 2012
if it does I don't know how to access it. u{w} = 4 5 10 (due to reversing the iteration)
Oleg Komarov
Oleg Komarov el 24 de Mayo de 2012
u{1}, u{2}, u{3} ...
Nick
Nick el 24 de Mayo de 2012
This does not work if one of the matrixes made is a 1x0 due to there being no output. But I still want to record this somehow.
Daniel Shub
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.
Nick
Nick el 24 de Mayo de 2012
Sorry I think I confused people. I edited my post to explain.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 24 de Mayo de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by