How to create an irregular Matrix

4 Ansichten (letzte 30 Tage)
JuanCarlosBodoque
JuanCarlosBodoque am 21 Aug. 2011
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

Antworten (2)

Image Analyst
Image Analyst am 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.
  1 Kommentar
JuanCarlosBodoque
JuanCarlosBodoque am 21 Aug. 2011
sorry for the explanation... i don't speak english very well.
I want to create a matrix with a random size (n*m)each row and column are from different size, for example if you consider a regular matrix of 5 columns and 5 rows, in a new matrix, my first column is size 2 columns of the regular matrix, the second 1 column and a third column of size 2 columns of a regular matrix. Same thing for the rows, for example my first row is 1 row of the regular matrix, the second is 3 rows of a regular matrix and the third is 1 row of the regular matrix (that's the reason of the picture).
When i have the irregular matrix created (with random size of columns and rows), i want to assign random numbers from 0 to 100 to the irregular matrix
Illustrating it in my irregular matrix only in the first row it could be something like:
A=[ 2 2 40 95 95 ]
I don't know if now is more clear
thanks

Melden Sie sich an, um zu kommentieren.


Oleg Komarov
Oleg Komarov am 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.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by