The are a few problems with your code. One is the square brackets in the expression MATRIX([cont1:p*11,cont2:q*11]). This builds a 1-D matrix which is then used as the indexing expression - not what you want. You need to give the row and column index expressions separately, so no square brackets.
Another is the arithmetic for cont1 and cont2. These get reset to 1 just before the assignment to MATRX, so they are always 1 at this point, which again isn't what you want.
Also, if you want the second SOL matrix to go in MATRIX(12:23, 1:71), you need to make the row index change fastest - that is, the row needs to change in the inner loop.
Other points: it's better to use variables to hold values of things like the matrix sizes, because then your code becomes far more flexible, and it's a good idea to preallocate the results matrix for efficiency.
Here is a version of your code that I think does what you want. Look at how cont1 and cont2 are initialised and incremented.
nr = 11;
nc = 71;
nblocksr = 9;
nblocksc = 9;
MATRIX = zeros(nblocksr*nr, nblocksc*nc);
cont2 = 1;
for q=1:nblocksc
cont1 = 1;
for p=1:nblocksr
SOL = rand(nr, nc);
MATRIX(cont1:cont1+nr-1, cont2:cont2+nc-1)=SOL;
cont1 = cont1+nr;
end
cont2 = cont2+nc;
end
Here's another way to do it - it's more concise because it doesn't use the extra variables but just works out where to put the results directly from the indices.
nr = 11;
nc = 71;
nblocksr = 9;
nblocksc = 9;
MATRIX = zeros(nblocksr*nr, nblocksc*nc);
for q=1:nblocksc
for p=1:nblocksr
SOL = rand(nr, nc);
MATRIX((p-1)*nr+1:p*nr, (q-1)*nc+1:q*nc)=SOL;
end
end
Best Answer