How to create random matrix with specified step in interval [a, b]

조회 수: 9 (최근 30일)
Reza Lashani
Reza Lashani 2022년 5월 22일
편집: Reza Lashani 2022년 5월 23일
Hello everyone. I want to create a matrix with size (m×n) and I want its entities be between 5 and 34 (or in interval [5,34]) with a step of 3. Which means it's only allowed to have 5, 8, 11, 14, ... . How can I create this matrix?

채택된 답변

Voss
Voss 2022년 5월 22일
편집: Voss 2022년 5월 22일
a = 5;
b = 34;
step = 3;
offset = 2;
n_levels = 1+floor((b-a)/step)
n_levels = 10
% for your reference, showing the min and max
% random values that will be generated:
offset + step*[1 n_levels]
ans = 1×2
5 32
% required size of resulting random matrix:
m = 10;
n = 12;
% generate the matrix using randi,
% [1 n_levels] maps to [5 32]
result = offset + step*randi([1 n_levels],m,n)
result = 10×12
8 8 11 23 26 23 8 20 20 32 32 26 5 26 11 20 8 14 11 29 32 23 23 11 32 20 20 11 23 17 11 17 29 5 23 11 17 5 11 32 26 14 20 8 11 20 23 20 26 8 17 20 20 14 8 14 32 14 8 14 29 32 32 23 17 14 23 8 8 26 29 14 23 17 23 11 29 8 5 11 26 26 23 26 17 26 29 17 29 23 17 14 11 20 29 23 32 8 29 32 20 14 32 26 5 8 17 17 11 11 5 32 23 14 8 8 20 8 17 17
  댓글 수: 4
Reza Lashani
Reza Lashani 2022년 5월 22일
You are correct. Thank you so much 👌🙏

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

추가 답변 (3개)

John D'Errico
John D'Errico 2022년 5월 22일
편집: John D'Errico 2022년 5월 22일
You can also use indexing. The idea is to generate random indexes.
ValidRandomSet = 5:3:34
ValidRandomSet = 1×10
5 8 11 14 17 20 23 26 29 32
m = 7;
n = 6;
ind = randi(numel(ValidRandomSet),[m,n])
ind = 7×6
3 10 4 5 6 7 5 4 2 10 7 6 6 10 2 10 8 6 3 5 1 10 8 10 7 7 9 6 4 6 3 3 2 5 9 10 1 8 9 10 7 3
X = ValidRandomSet(ind)
X = 7×6
11 32 14 17 20 23 17 14 8 32 23 20 20 32 8 32 26 20 11 17 5 32 26 32 23 23 29 20 14 20 11 11 8 17 29 32 5 26 29 32 23 11
The virtue of such a scheme is it works for any target set of numbers.
  댓글 수: 1
Reza Lashani
Reza Lashani 2022년 5월 23일
편집: Reza Lashani 2022년 5월 23일
very nice. I could write this by using a foor loop. but your way to doing it, is much better. thank you for your help.
This is what I wrote:
function out = myfun(Step, a, b, m, n)
out = zeros(m, n);
ValidSet = a: Step: b;
for i = 1: m*n
ind = randi(length(ValidSet));
out(i) = ValidSet(ind);
end
end

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


Jonas
Jonas 2022년 5월 22일
편집: Jonas 2022년 5월 22일
generate a matrix with random integer values using randi. The spacing will be 1, you can then multiply the values by 3 to get spacing of 3. then shift your values towards your interval by addition
randi(10, 20,30)*3+2
max value will be 32 and minimum value 5

Image Analyst
Image Analyst 2022년 5월 22일
Here's another way. You can create your random numbers as floating point then use discretize them into numbers in the set you specify:
m = 6
n = 18
% Get the random numbers
r = 5 + (35-5) * rand(m, n)
% Discretize into nearest integer that is in the set
% [5 8 11 14 17 20 23 26 29 32]
edges = 5:3:35
values = edges(1:end-1)
r2 = discretize(r, edges, values)

카테고리

Help CenterFile Exchange에서 Random Number Generation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by