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

1 view (last 30 days)
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 Comment
Image Analyst
Image Analyst on 25 Aug 2020
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,

Sign in to comment.

Answers (2)

Brandon Eidson
Brandon Eidson on 5 Oct 2016
Edited: Brandon Eidson on 6 Oct 2016
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 on 6 Oct 2016
R = 5*rand(4);
A = zeros(size(R));
A(R>=1 & R <= 2) = 1;
B = zeros(size(R));
B(R>=3 & R <= 4) = 2;

Categories

Find more on Numeric Types 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!