How to concatenate smaller matrices to form a larger matrix? - matlab

I would like to concatenate 14641 number of 3X3 matrices into a matrix of size 363X363 and the large matrix must contain 121 submatrices in each row(121*3=363 columns) and 121 such rows of submatrices(121*3=363rows).I have gone through the similar questions but I didn't get the correct logic to concatenate large number of matrices.
Awaiting your suggestions. Thanks in advance.
PS: I got those 3X3 matrices from a 363X363 matrix. The following code is for splitting the single matrix into submatrices.
I=imread('photo.jpg');
B = randi([0 255],363,726,3);
B(1:numel(I)) = I;
L=B(1:363,1:363);
[al,bl]=size(L);
ImageSize=al*bl;
BlockD=3; % i assume 3x3 block
BlockSize=BlockD*BlockD;
TotalBlocks=ImageSize/BlockSize;
subL=zeros(BlockD,BlockD,TotalBlocks); %arrays of blocks.
LL=double(L);
k=1;
for i=1:BlockD:al
for j=1:BlockD:bl
subL(:,:,k)=LL(i:i+BlockD-1,j:j+BlockD-1);
k=k+1;
end
end
Now I want to concatenate all these 'subL' submatrices to form 'LL' again
Using blocproc instead of above code
I tried using blockproc function instead of the above code. I did this piece of code and is working pretty well.Thank you
I=imread('photo.jpg');
B = randi([0 255],363,726,3);
B(1:numel(I)) = I;
L=B(1:363,1:363);
q=[1 2 3 4];
fun=#(block_struct)quaternionrotate(q,block_struct.data);
LL = blockproc(L,[3 3],fun);
and the function quaternionrotate is
function [ Lrot1 ] = quaternionrotate(q,A)
qinv=quatinv(q);
B=zeros(3,1);
A1=[B A];
Lrot=quatmultiply(q,quatmultiply(A1,qinv));
Lrot(:,1)=[];
Lrot1=Lrot;
end

Finally figured this out!
% I used this to make 14641 3x3 sample matrices, you should use your own
matrix = ones(3,3);
for i = 2:14641
matrix = [matrix, i*ones(3,3)];
end
c_size = 121;
output = cell(c_size, c_size) % Make 121x121 cell matrix
% Populate the cell matrix
for i = 1:1:c_size
for j = 1:1:c_size
output(i,j) = {matrix(1:3, (i-1)*c_size*3+(j-1)*3+1 : (i-1)*c_size*3+(j-1)*3+3)};
end
end
This breaks up the 43923x3 size matrix of 363 3x3 matrices into a 121x121 cell matrix of 3x3 matrices. Now that's a tongue twister...
Either way the code does what you need! :)

Related

How to vectorize code with 3 loops that generates a matrix

Could you help me to vectorise this Matlav code constructing a matrix A of dimension MNx(2+N-1)xR in order to speed it up? At the moment it takes approx. 8 sec
INITIALISING
R=200;
M=400;
N=20;
B=[kron((1:1:M)', ones(N,1)) repmat((1:1:N)', M,1)]; %(MN)x(2)
B looks like
B=[1 1;
1 2;
...;
1 20;
2 1;
2 2;
...
2 20;
...
400 20]
CODE
A=[ repmat(B,1,1,R) zeros(M*N,N-1,R)]; %Allocate the space
%(MN)x(2+N-1)x(R)
% I want A(:,1:2,r)=B for r=1,...,R
%Fill the remaining columns of A in the following way
for r=1:R
utemp1=randn(M*N, N-1); %Generate a matrix of random
%numbers of dimension (M*N)x(N-1)
utemp2=randn(M, N); %Generate a matrix of random
%numbers of dimension (M)x(N)
utemp3=zeros(M*N,N-1); %Generate a matrix of random
%numbers of dimension(M)x(N-1)
for m=1:M
for j=1:N
utemp3((m-1)*N+j,:)= utemp2(m,j)+[utemp2(m,1:j-1) utemp2(m,j+1:N)]; %(1)x(N-1)
%e.g. if m=2, j=3, I want to fill the 23th row of utemp3
%with utemp2(2,3)+[utemp2(2,1:2) utemp2(m,4:20)];
%e.g. if m=4, j=1, I want to fill the 61st row of utemp3
%with utemp2(4,1)+[utemp2(4,2:20)];
end
end
A(:,3:end,r)=utemp1+utemp3; %sum utemp1
end
SOME EXPLANATIONS
for r=1,...,R
A is such that
for m=1,...,M and for j=1,...,N
the row in A(:,:,r) starting with [m j] in the first two columns is filled in the remaining (N-1) columns with uj+uh+ujh for each h~=j, where uj, uh, ujh are i.i.d standard Gaussian numbers that can be found in utemp1 and utemp2.
You can precompute the indices and use them in 500 iterations:
idx = repmat(reshape(1:M*N,M,N).',1,N);
idx = reshape(idx(logical(kron(~eye(N),ones(1,M)))),N-1,[]).';
for k=1:500
for r=1:R
utemp2=randn(M*N,1);
A(:,3:end,r)=randn(M*N, N-1)+bsxfun(#plus,utemp2,utemp2(idx) );
end
end
However allocating large matrices ,specially of repeated elements, and vectorizing operations on it is not always the most efficient way. There are built-in functions that directly operate on the original array avoiding repeating elements of the array.

Merge matrixes that are generated by function [duplicate]

In MatLab, I have a matrix SimC which has dimension 22 x 4. I re-generate this matrix 10 times using a for loop.
I want to end up with a matrix U that contains SimC(1) in rows 1 to 22, SimC(2) in rows 23 to 45 and so on. Hence U should have dimension 220 x 4 in the end.
Thank you!!
Edit:
nTrials = 10;
n = 22;
U = zeros(nTrials * n , 4) %Dimension of the final output matrix
for i = 1 : nTrials
SimC = SomeSimulation() %This generates an nx4 matrix
U = vertcat(SimC)
end
Unfortunately the above doesn't work as U = vertcat(SimC) only gives back SimC instead of concatenating.
vertcat is a good choice, but it will result in a growing matrix. This is not good practice on larger programs because it can really slow down. In your problem, though, you aren't looping through too many times, so vertcat is fine.
To use vertcat, you would NOT pre-allocate the full final size of the U matrix...just create an empty U. Then, when invoking vertcat, you need to give it both matrices that you want to concatenate:
nTrials = 10;
n = 22;
U = [] %create an empty output matrix
for i = 1 : nTrials
SimC = SomeSimulation(); %This generates an nx4 matrix
U = vertcat(U,SimC); %concatenate the two matrices
end
The better way to do this, since you already know the final size, is to pre-allocate your full U (as you did) and then put your values into U via computing the correct indices. Something like this:
nTrials = 10;
n = 22;
U = U = zeros(nTrials * n , 4); %create a full output matrix
for i = 1 : nTrials
SimC = SomeSimulation(); %This generates an nx4 matrix
indices = (i-1)*n+[1:n]; %here are the rows where you want to put the latest output
U(indices,:)=SimC; %copies SimC into the correct rows of U
end

Finding indices of matrices while traversing

I have a matrix:
1|2|3|4
4|5|6|7
7|8|9|10
10|11|12|13
I want to multiply the indices of this matrix with indices of another matrix of different size:
7|8|9
9|10|10
10|11|11
for these two matrices I have used the following for loops:
for x=1:4
for y=1:4
for m=1:3
for n=1:3
c=(m*x+n*y);
end
end
end
end
Is there any way to rewrite the above code without using loops? If the indices of each element can be generated in the above matrices, I think it can be done. Please help
mx = m'*x;
mx = mx(:);
ny = n'*y;
ny = ny(:);
mxe = repmat(mx, [length(ny), 1]);
nye = repmat(ny, [length(mx), 1]);
c = mxe+nye;
This will result in c containing all the values that get put in during that loop you have there (note that in your loop, value gets assigned and overwritten).

How can I build a Scilab / MATLAB program that averages a 3D matrix?

I need to make a scilab / MATLAB program that averages the values of a 3D matrix in cubes of a given size(N x N x N).I am eternally grateful to anyone who can help me.
Thanks in advance
In MATLAB, mat2cell and cellfun make a great team for working on N-dimensional non-overlapping blocks, as I think is the case in the question. An example scenario:
[IN]: A = [30x30x30] array
[IN]: bd = [5 5 5], size of cube
[OUT]: B = [6x6x6] array of block means
To accomplish the above, the solution is:
dims = [30 30 30]; bd = [5 5 5];
A = rand(dims);
f = floor(dims./bd);
remDims = mod(dims,bd); % handle dims that are not a multiple of block size
Ac = mat2cell(A,...
[bd(1)*ones(f(1),1); remDims(1)*ones(remDims(1)>0)], ....
[bd(2)*ones(f(2),1); remDims(2)*ones(remDims(2)>0)], ....
[bd(3)*ones(f(3),1); remDims(3)*ones(remDims(3)>0)] );
B = cellfun(#(x) mean(x(:)),Ac);
If you need a full size output with the mean values replicated, there is a straightforward solution involving the 'UniformOutput' option of cellfun followed by cell2mat.
If you want overlapping cubes and the same size output as input, you can simply do convn(A,ones(blockDims)/prod(blockDims),'same').
EDIT: Simplifications, clarity, generality and fixes.
N = 10; %Same as OP's parameter
M = 10*N;%The input matrix's size in each dimensiona, assumes M is an integer multiple of N
Mat = rand(M,M,M); % A random input matrix
avgs = zeros((M/N)^3,1); %Initializing output vector
l=1; %indexing
for i=1:M/N %indexing 1st coord
for j=1:M/N %indexing 2nd coord
for k=1:M/N % indexing third coord
temp = Mat((i-1)*N+1:i*N,(j-1)*N+1:j*N,(k-1)*N+1:k*N); %temporary copy
avg(l) = mean(temp(:)); %averaging operation on the N*N*N copy
l = l+1; %increment indexing
end
end
end
The for loops and copying can be eliminated once you get the gist of indexing.

How to make a block of matrices from large matrix in matlab?

I have a 256x256 matrix from that I want to create block of 8x8 matrices. I have a below code which shows just one block i want all the blocks then subtract a number from each element of the matrix. When i do the subtract 128 from each element it doesn't show negative values rather showing only 0.
[x,y] = size(M);
for i=1:8
for j=1:8
k(i,j) = M(i,j);
end
end
disp(k);
for a=1:size(k)
for b=1:size(k)
A(a,b) = k(a,b) - 128;
end
end
disp(A);
well, if you have to subtract a fixed value it's better to do it as
M = M - 128;
It's faster.
You said you get 0 values instead of negative ones; it is likely due to the type of the matrix which is unsigned (i.e. the guys are just positive). A cast to general integer is necessary. Try:
M = int16(M) - 128;
To obtain the partition I propose the following, there can be more efficient ways, btw:
r = rand(256,256); %// test case, substitute with your matrix M
[i j] = meshgrid(1:8:256); %// lattice of indices
idx = num2cell([ i(:) , j(:) ],2); %// cell version
matr = cellfun( #(i) r(i(1):i(1)+7,i(2):i(2)+7), idx, 'UniformOutput',false); %// blocks
matr will contain all the sub-matrices in lexicographical order. e.g.
matr{2}
ans =
0.4026 0.3141 0.4164 0.5005 0.6952 0.1955 0.9803 0.5097
0.8186 0.9280 0.1737 0.6133 0.8562 0.7405 0.8766 0.0975
0.2704 0.8333 0.1892 0.7661 0.5168 0.3856 0.1432 0.9958
0.9973 0.8488 0.6937 0.2630 0.1004 0.5842 0.1844 0.5206
0.4052 0.0629 0.6982 0.1530 0.9234 0.1271 0.7317 0.3541
0.2984 0.3633 0.1510 0.0297 0.0225 0.7945 0.2925 0.0396
0.5097 0.0802 0.8744 0.1032 0.8523 0.6150 0.4845 0.5703
0.8635 0.0194 0.1879 0.5017 0.5297 0.6319 0.2406 0.5125
Explanation: In matlab you can efficiently operate on matrices and slices of them. For instance, you can easily copy submatrices, e.g. the first 8x8 matrix is
sub = M(1:8,1:8);
You want all the submatrices, thus you need kind of a lattice of indices to get
sub_ij = M(1+8*(i-1) : 7 * 8*(i-1) , 1+8*(j-1) : 7 * 8*(j-1))
i.e. you need the lattice
(1+8*(i-1) : 7 * 8*(i-1) , 1+8*(j-1) : 7 * 8*(j-1)) % // for all i,j
you can use meshgrid for that.
Finally you have to cut off the pieces, that is what the last two instruction do. particularly the first one generates the indices (try idx{1},...), and the second one generates the submatrices (try matr{1},...).
Hope this code helps you. It has 16x16 image to divide into 4x4 blocks. You may change the value and get the required output as you like.