changing data based on values

2 views (last 30 days)
Daniel Ring
Daniel Ring on 24 Dec 2018
Answered: Image Analyst on 24 Dec 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.

Accepted Answer

Image Analyst
Image Analyst on 24 Dec 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.

More Answers (0)

Categories

Find more on Author Block Masks in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!