how to split a large matrix into smaller matrices and assign random values

조회 수: 3 (최근 30일)
I have a 180x180 matrix of 0's and I was to assign different range of random integers to each 45x45 set in the matrix. I have show the example output that I want for a much smaller matrix (10x10) that breaks it up into 5x5 matrices of different random values below. Any easy way to do this that doesn't involve a million for loops? I just want to expand this code to an N = zeros(180,180) type deal with 45x45 subparts.
N = zeros(10,10);
for i = 1:5
for j = 1:5
N(i,j) = randi([1,10]);
end
end
for i = 1:5
for j = 6:10
N(i,j) = randi([15,100])
end
end
for i = 6:10
for j = 6:10
N(i,j) = randi([1,17])
end
end
for i = 6:10
for j = 1:5
N(i,j) = randi([100,250])
end
end
N

채택된 답변

Walter Roberson
Walter Roberson 2017년 2월 27일
low_vals = [1 15 1 100;
8 -4 17 3;
1 2 9 15;
0 -8 3 72];
high_vals = [10 100 17 250;
14 11 84 9;
33 11 99 222;
4 -3 88 105];
N = cell2mat( arrayfun(@(L, H) randi([L, H], 45, 45), low_vals, high_vals, 'Uniform', 0) );
The above would create 4 x 4 of 45 x 45 subblocks.
  댓글 수: 3
Walter Roberson
Walter Roberson 2017년 2월 27일
Did you run the above code? It creates a 180 x 180 matrix in which each 45 x 45 (non-overlapped) sub-block is based on randi() between the corresponding low value and high value. For example, the third sub-block over on the second row of sub-blocks would be based on randi([17, 84], 45, 45) in the above demonstration code.
Andrew Poissant
Andrew Poissant 2017년 2월 27일
Nvm, I was able to use the cell2mat for my question. Thanks!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by