How to create an irregular Matrix
4 views (last 30 days)
Show older comments
Hi, I have a question. I want to know how to create an array of irregular random size, that is me for example when considering a regular array of size nxn, I consider for example that the first 4 squares (A11, A12, A21, A22) represent a single square, the square next square in the same row size is A13 A23 and so on until all the nxn spaces. The requirement is to respect both the length and width of the spaces (if assigned to a row 2 squares, the entire row will contain 2 squares), as well as for columns. These assignments (to be random), I want to assign a number aleatorrio within, for example if I have a large square 2x2, assign this great square a random number. Associate an image with colors exemplifying what I mean, each large square you want to randomly assign a number, for example the yellow square you want to assign more than April 1 thank you very much
0 Comments
Answers (2)
Image Analyst
on 21 Aug 2011
Not sure I followed all of your explanation. But your picture looks pretty random to me. I think you'd be best off using brute force to construct this Mondrian of an array, perhaps with some help from repmat(). For example
AB = [A B];
CD = [C; D];
Just make sure the rows or columns agree when you do the horizontal or vertical stitching.
Oleg Komarov
on 21 Aug 2011
% lenght of the side of the square
l = 17;
% Number of row sub-blocks
m = 5;
% Number of column sub-blocks
n = 4;
% Random dimensions of sub blocks
while true
md = randi([1 l],m,1);
nd = randi([1 l],1,n);
md = round(md/sum(md)*l);
nd = round(nd/sum(nd)*l);
if sum(md) == l && sum(nd) == l && all(md) && all(nd)
break
end
end
% Build matrix
mdv = zeros(l,1);
mdc = cumsum(md);
mdv([1; mdc(1:end-1)+1]) = 1;
ndv = zeros(1,l);
ndc = cumsum(nd);
ndv([1 ndc(1:end-1)+1]) = 0:n-1;
out = bsxfun(@plus, cumsum(mdv),cumsum(ndv));
% Visualize matrix
imagesc(out)
EDIT
To have bigger dispersion in the dimensions of the sub-blocks when calling randi([1 imax],...) set imax to a bigger value but be aware that the bigger the imax the more likely the condition to break the loop is NOT satisfied, i.e. it will take longer to generate random sub-blocks.
0 Comments
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!