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