changing data based on values
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Daniel Ring
le 24 Déc 2018
Réponse apportée : Image Analyst
le 24 Déc 2018
I have a very long string of data, and I want to change each value based on if it is in between certain values in another set of data.
for example, my string of data is 1, 5, 5, 3, 6, 8. I want to change those numbers to 1 if they are between 1 and 3, 2 if they are between 4 and 6, and 3 if they are between 7 and 9. The result should be 1, 2, 2, 1, 2, 3.
In my code, I have the very long string of data that I want to change as X, the range data as Y, and what I want to change it to as Z. The code I came up with (which doesn't work) is:
X(Y(1,:)<X(:,1)<Y(2,:),1) = Z(1,:);
X(Y(2,:)<X(:,1)<Y(3,:),1) = Z(2,:);
X(Y(3,:)<X(:,1)<Y(4,:),1) = Z(3,:);
Etc.
0 commentaires
Réponse acceptée
Image Analyst
le 24 Déc 2018
If they're all integers, you can use intlut(). Otherwise you can do it one range at a time by masking
vCopy = v; % Make copy so you don't change already-changed values, only original values.
mask = v >= 1 & v <= 3;
vCopy(mask) = 1;
mask = v >= 4 & v <= 6;
vCopy(mask) = 2;
and so on.
If there is some method to how you're deciding on the ranges and assignment values, then you can put it into a loop.
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Characters and Strings 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!