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

1 visualización (últimos 30 días)
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 comentario
Image Analyst
Image Analyst el 25 de Ag. de 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,

Iniciar sesión para comentar.

Respuestas (2)

Brandon Eidson
Brandon Eidson el 5 de Oct. de 2016
Editada: Brandon Eidson el 6 de Oct. de 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 el 6 de Oct. de 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;

Categorías

Más información sobre Numeric Types en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by