replace values in an array with certain numbers

I have an array (very long array) with values varying from 1 to 20 (with decimal values in between). What i want to do is the following:
if the values in my array are between 0 and 1, then replace with number 500;
if the values are between 1 and 2, then replace with 1000;
if the values are between 2 and 3, then replace with 1500;
..... and so on.
How could i do this, thanks in advance :)

 채택된 답변

KSSV
KSSV 2020년 12월 14일

1 개 추천

You can get the logical indices by using:
idx = A >=0 & A< 1 ; % logical indices
A(idx) = 500 ; % repalces them
Follow the same with others.

댓글 수: 2

This is likely the best way. It's probably possible to use a loop to avoid code getting too out of hand:
for val = 1:20
idx = (A >= val-1) && (A < val);
A(idx) = val*500;
end
both worked, thank you :)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2020년 12월 15일
x = 2*rand(10, 1);
edges = 0:2;
d = discretize(x, edges, [500, 1000]);
results = table(x, d, 'VariableNames', ["Raw data", "Discretized value"])
results = 10x2 table
Raw data Discretized value ________ _________________ 0.69853 500 1.5357 1000 0.18276 500 1.2076 1000 1.5088 1000 0.030049 500 0.049193 500 1.3234 1000 0.23645 500 1.2114 1000

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

질문:

2020년 12월 14일

댓글:

2020년 12월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by