finding elements in array
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have an array
vk = [0 0.025 0.05 0.075 0.1 0.125 0.15 0.175].
I am writing this line
op= vk(vk>= 0.05 & vk<= 0.175)
The result which I am getting is
op =
0.0750 0.1000 0.1250 0.1500 0.1750
which is wrong, I should get
0.05 0.0750 0.1000 0.1250 0.1500 0.1750
does anyone know what I am doing wrong?
Also how I can get the indices number of the array vk and of op?
Réponse acceptée
Meg Noah
le 10 Jan 2020
To get the indices to the vk array
vk = [0 0.025 0.05 0.075 0.1 0.125 0.15 0.175];
[idx] = find(vk>= 0.05 & vk<= 0.175);
op = vk(idx)
4 commentaires
Meg Noah
le 11 Jan 2020
Modifié(e) : Meg Noah
le 11 Jan 2020
The OP specifically asks: Also how I can get the indices number of the array vk and of op? And that is why I have a 'find' command.
When I run this code to find the indices of the vk meeting that condition, this is what is displayed:
>> idx = find(vk>=0.05 & vk<=0.175)
idx =
3 4 5 6 7 8
Remember: matlab arrays begin indexing at position 1. There is no 0th element.
>> vk(0)
Array indices must be positive integers or logical values.
The value of element 1 is 0. Since it does not meet the condition, the index 1 is not returned on the list of indices that meet the condition.
The value of element 2 is 0.025. Since it does not meet the condition, the index 2 is not returned on the list of indices that meet the condition.
The value of element 3 is 0.025. It is the first value that meets the condition, and index 3 is the first value returned on the list of indices that meet that condition.
If your problem is floating point, then maybe try recasting it. Is it being set to a double precision array initially?
vk = double(vk);
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Array Geometries and Analysis 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!