Comparing a number matrix to certain values and storing it in another array / matrix

조회 수: 1 (최근 30일)
Hello
To give a background on what im working on. I have a 20x20 matrix that consists of numbers between 1 and 5, i want to be able compare each number to a certain range so if the range was 1-2 i'd want all number between 1 and 2 inclusive to display a number 1. If the range was 3-4 i want all numbers that fall in the range to display a number 2 in another 20x20 matrix. In essence i want to display another matrix showing numbers that fall within each range and using a given display value for it,
  댓글 수: 1
Image Analyst
Image Analyst 2020년 8월 25일
Original question, in case he deletes it like he's done with other posts:
Hello
To give a background on what im working on. I have a 20x20 matrix that consists of numbers between 1 and 5, i want to be able compare each number to a certain range so if the range was 1-2 i'd want all number between 1 and 2 inclusive to display a number 1. If the range was 3-4 i want all numbers that fall in the range to display a number 2 in another 20x20 matrix. In essence i want to display another matrix showing numbers that fall within each range and using a given display value for it,

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

답변 (2개)

Brandon Eidson
Brandon Eidson 2016년 10월 5일
편집: Brandon Eidson 2016년 10월 6일
Hey Wilhelm,
Because your ranges are from one integer to the next, you can accomplish your desired workflow using the "floor" command. Its documentation is below.
https://www.mathworks.com/help/matlab/ref/floor.html
Here is a brief example that creates a matrix with random numbers between 1 and 5, then uses the "floor" method to bin the numbers to the closest, smaller integer.
>> example = 1+(4*(rand(3)))
example =
1.3034 4.1167 3.2753
1.2158 4.7360 2.8776
3.1232 1.5196 1.0476
>> floor(example)
ans =
1 4 3
1 4 2
3 1 1

Thorsten
Thorsten 2016년 10월 6일
R = 5*rand(4);
A = zeros(size(R));
A(R>=1 & R <= 2) = 1;
B = zeros(size(R));
B(R>=3 & R <= 4) = 2;

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by