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

4 ビュー (過去 30 日間)
Matlabhelp
Matlabhelp 2016 年 9 月 29 日
コメント済み: Image Analyst 2020 年 8 月 25 日
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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by