필터 지우기
필터 지우기

Is there a way to set the range of values when using the magic function?

조회 수: 1 (최근 30일)
Ryan O Malley
Ryan O Malley 2022년 4월 8일
편집: Adam Danz 2022년 4월 8일
Lets say I have
M=magic(5)
The values randomly assigned to each positon in the matrix will range from 1 to 25.
Is there any way to generate a 5x5 matrix like this but to control the range of each element, for example 1 to 6?

답변 (1개)

Adam Danz
Adam Danz 2022년 4월 8일
편집: Adam Danz 2022년 4월 8일
The values returned by magic(n) are not randomly assigned (see documentation).
I think what you're describing is randperm.
n = reshape(randperm(25),5,5) % values 1:25
n = 5×5
10 8 6 20 18 15 4 17 21 16 14 7 9 12 22 1 25 13 2 23 19 3 5 24 11
or perhaps randi for sampling with replacement
n = randi(6,5,5) % values 1:6
n = 5×5
1 1 6 4 2 3 6 2 4 5 2 2 5 4 1 3 3 4 2 6 1 1 3 6 4

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by