changing data based on values

3 次查看(过去 30 天)
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.

采纳的回答

Image Analyst
Image Analyst 2018-12-24
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 个)

类别

Help CenterFile Exchange 中查找有关 Author Block Masks 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by