필터 지우기
필터 지우기

changing data based on values

조회 수: 3 (최근 30일)
Daniel Ring
Daniel Ring 2018년 12월 24일
답변: Image Analyst 2018년 12월 24일
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