# While loop for testing the end of vector (Matlab)

5 vues (au cours des 30 derniers jours)
Afluo Raoual le 17 Fév 2021
Commenté : Afluo Raoual le 17 Fév 2021
Dear members,
If we have a long vector that we don't know its length, and we want use while loop to repeat the task than stop and exit from the loop when the vector length is at its end.
For example, if the vector length is 10000 bits so we repeat the calculation (of any task) until 10000 than we stop and exit.
But in my case, I don't know the length of my vector. So how can I program this please?
Thank you.
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Rik le 17 Fév 2021
You can use numel to create a loop over a vector of unknown size:
A=rand(1,100);
for n=1:numel(A) % n will go to 100
end
##### 5 commentairesAfficher 3 commentaires plus anciensMasquer 3 commentaires plus anciens
Rik le 17 Fév 2021
If your issue is not yet resolved, feel free to comment with your remaining issues.
Afluo Raoual le 17 Fév 2021
It's done

Connectez-vous pour commenter.

### Plus de réponses (1)

Daniel Pollard le 17 Fév 2021
Modifié(e) : Daniel Pollard le 17 Fév 2021
If you know that it's 1-dimensional, you can use
numel(vector)
which returns the length of your vector. Also useful to know is
size
which returns the size of a matrix (eg, [2 2] for a 2x2 matrix, [10 1] for a 10x1 vector, etc).
Edit I've replaced length with numel - thank you Rik.
##### 3 commentairesAfficher 1 commentaire plus ancienMasquer 1 commentaire plus ancien
Daniel Pollard le 17 Fév 2021
This is news to me - I've used length multiple times without any issues. What exactly is the bug?
Rik le 17 Fév 2021
The bug is not with length, the bug is with the use of length. For a vector it is equivalent to numel, and for array inputs it is probably not what you mean. How often did you want to loop over max(size(A)) without knowing which dimension is that maximum?

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by