Extract and then sum sub-matrices in Matlab [duplicate] - matlab

Given a square matrix of say size 400x400, how would I go about splitting this into constituent sub-matrices of 20x20 using a for-loop? I can't even think where to begin!
I imagine I want something like :
[x,y] = size(matrix)
for i = 1:20:x
for j = 1:20:y
but I'm unsure how I would proceed. Thoughts?

Well, I know that the poster explicitly asked for a for loop, and Jeff Mather's answer provided exactly that.
But still I got curious whether it is possible to decompose a matrix into tiles (sub-matrices) of a given size without a loop. In case someone else is curious, too, here's what I have come up with:
T = permute(reshape(permute(reshape(A, size(A, 1), n, []), [2 1 3]), n, m, []), [2 1 3])
transforms a two-dimensional array A into a three-dimensional array T, where each 2d slice T(:, :, i) is one of the tiles of size m x n. The third index enumerates the tiles in standard Matlab linearized order, tile rows first.
The variant
T = permute(reshape(A, size(A, 1), n, []), [2 1 3]);
T = permute(reshape(T, n, m, [], size(T, 3)), [2 1 3 4]);
makes T a four-dimensional array where T(:, :, i, j) gives the 2d slice with tile indices i, j.
Coming up with these expressions feels a bit like solving a sliding puzzle. ;-)

I'm sorry that my answer does not use a for loop either, but this would also do the trick:
cellOf20x20matrices = mat2cell(matrix, ones(1,20)*20, ones(1,20)*20)
You can then access the individual cells like:
cellOf20x20matrices{i,j}(a,b)
where i,j is the submatrix to fetch (and a,b is the indexing into that matrix if needed)
Regards

You seem really close. Just using the problem as you described it (400-by-400, divided into 20-by-20 chunks), wouldn't this do what you want?
[x,y] = size(M);
for i = 1:20:x
for j = 1:20:y
tmp = M(i:(i+19), j:(j+19));
% Do something interesting with "tmp" here.
end
end

Even though the question is basically for 2D matrices, inspired by A. Donda's answer I would like to expand his answer to 3D matrices so that this technique could be used in cropping True Color images (3D)
A = imread('peppers.png'); %// size(384x512x3)
nCol = 4; %// number of Col blocks
nRow = 2; %// number of Row blocks
m = size(A,1)/nRow; %// Sub-matrix row size (Should be an integer)
n = size(A,2)/nCol; %// Sub-matrix column size (Should be an integer)
imshow(A); %// show original image
out1 = reshape(permute(A,[2 1 4 3]),size(A,2),m,[],size(A,3));
out2 = permute(reshape(permute(out1,[2 1 3 4]),m,n,[],size(A,3)),[1 2 4 3]);
figure;
for i = 1:nCol*nRow
subplot(nRow,nCol,i); imshow(out2(:,:,:,i));
end
The basic idea is to make the 3rd Dimension unaffected while reshaping so that the image isn't distorted. To achieve this, additional permuting was done to swap 3rd and 4th dimensions. Once the process is done, the dimensions are restored as it was, by permuting back.
Results:
Original Image
Subplots (Partitions / Sub Matrices)
Advantage of this method is, it works good on 2D images as well.
Here is an example of a Gray Scale image (2D). Example used here is MatLab in-built image 'cameraman.tif'

With some many upvotes for the answer that makes use nested calls to permute, I thought of timing it and comparing to the other answer that makes use of mat2cell.
It is true that they don't return the exact same thing but:
the cell can be easily converted into a matrix like the other (I timed this, see further down);
when this problem arises, it is preferable (in my experience) to have the data in a cell since later on one will often want to put the original back together;
Anyway, I have compared them both with the following script. The code was run in Octave (version 3.9.1) with JIT disabled.
function T = split_by_reshape_permute (A, m, n)
T = permute (reshape (permute (reshape (A, size (A, 1), n, []), [2 1 3]), n, m, []), [2 1 3]);
endfunction
function T = split_by_mat2cell (A, m, n)
l = size (A) ./ [m n];
T = mat2cell (A, repmat (m, l(1), 1), repmat (n, l (2), 1));
endfunction
function t = time_it (f, varargin)
t = cputime ();
for i = 1:100
f(varargin{:});
endfor
t = cputime () - t;
endfunction
Asizes = [30 50 80 100 300 500 800 1000 3000 5000 8000 10000];
Tsides = [2 5 10];
As = arrayfun (#rand, Asizes, "UniformOutput", false);
for d = Tsides
figure ();
t1 = t2 = [];
for A = As
A = A{1};
s = rows (A) /d;
t1(end+1) = time_it (#split_by_reshape_permute, A, s, s);
t2(end+1) = time_it (#split_by_mat2cell, A, s, s);
endfor
semilogy (Asizes, [t1(:) t2(:)]);
title (sprintf ("Splitting in %i", d));
legend ("reshape-permute", "mat2cell");
xlabel ("Length of matrix side (all squares)");
ylabel ("log (CPU time)");
endfor
Note that the Y axis is in log scale
Performance
Performance wise, using the nested permute will only be faster for smaller matrices where big changes in relative performance are actually very small changes in time. Note that the Y axis is in log scale, so the difference between the two functions for a 100x100 matrix is 0.02 seconds while for a 10000x10000 matrix is 100 seconds.
I have also tested the following which will convert the cell into a matrix so that the return values of the two functions are the same:
function T = split_by_mat2cell (A, m, n)
l = size (A) ./ [m n];
T = mat2cell (A, repmat (m, l(1), 1), repmat (n, l (2), 1), 1);
T = reshape (cell2mat (T(:)'), [m n numel(T)]);
endfunction
This does slow it down a bit but not enough to consider (the lines will cross at 600x600 instead of 400x400).
Readability
It is so much more difficult to get your head around the use of the nested permute and reshape. It's mad to use it. It will increase maintenance time by a lot (but hey, this is Matlab language, it's not supposed to be elegant and reusable).
Future
The nested calls to permute does not expand nicely at all into N dimensions. I guess it would require a for loop by dimension (which would not help at all the already quite cryptic code). On the other hand, making use of mat2cell:
function T = split_by_mat2cell (A, lengths)
dl = arrayfun (#(l, s) repmat (l, s, 1), lengths, size (A) ./ lengths, "UniformOutput", false);
T = mat2cell (A, dl{:});
endfunction
Edit (and tested in Matlab too)
The amount of upvotes on the answer suggesting to use permute and reshape got me so curious that I decided to get this tested in Matlab (R2010b). The results there were pretty much the same, i.e., it's performance is really poor. So unless this operation will be done a lot of times, in matrices that will always be small (less than 300x300), and there will always be a Matlab guru around to explain what it does, don't use it.

If you want to use a for loop you can do this:
[x,y] = size(matrix)
k=1; % counter
for i = 1:20:x
for j = 1:20:y
subMatrix=Matrix(i:i+19, j:j+19);
subMatrixCell{k}=subMatrix; % if you want to save all the
% submatrices into a cell array
k=k+1;
end
end

Related

3D matrix, multiplication on last dimensions

I have a 3D-matrix A, with size lets say 3x12x100. The first two dimensions define 3×12 matrices, the latter one is simply the linear index. I want a very simple operation on these 100 matrices. For all these matrices, i want them multiplied with its conjugate transpose. With a very simple for loop, i can create this:
data = data;
A = zeros(100, 12, 12);
for i=1:100
A(i, :, :) = data(:, :, i)'*data(:, :, i);
end
But i like clean code, so i dont really prefer this for-loop. I have done some searching and sometimes find something like mtimesx (which is a custom made MATLAB function from 2010). I think i am missing something very obvious (as usual), because this seems a fairly easy operation (its just an "element-wise" matrix multiplication).
The size of my actual matrix is 3x12x862400. My original script takes about 10 minutes or longer, a variant on what #FangQ posts fixes it in a matter of seconds. My new code is as following, note that it still is under construction and i still need to validate it:
data = rand(3, 12, 862400) + i*rand(3, 12, 862400)
data2 = conj(permute(data, [2 1 3])); % conjugate transpose each matrix
% my data matrix contains 862400 3x12 matrices with complex numbers
Ap = permute(data2, [2 1 4 3]);
Bp = permute(data, [1 4 2 3]);
M = Ap.*Bp;
M = sum(M, 1);
M = permute(M, [2 3 4 1]);
#Cris was right, you can find an example from this MatlabCentral post
https://www.mathworks.com/matlabcentral/answers/10161-3d-matrix-multiplication#answer_413531

How to stack submatrices of a matrix to reduce dimensionality

I couldn't find a good solution for this:
I have a matrix of this size: 60x10x3
And I want to transform it into a matrix of this size: 600x3
Basically I want to stack the 10 matrices with the dimension 60x3 next to each other in the first dimension.
How can I achieve that elegantly in matlab?
It seems that I don't understand what the intended output is, but here are two different ways to achieve it. I added the loop to create different numbers so the results can be studied.
Method 1 - Stack Vertically or Horizontally
s = zeros(60,10,3);
for x = 1:9
s(:,x,:) = x;
end
t = reshape(s, 600, 3); %vert
u = t'; %hori
Method 2 - Stack 3rd dimension Vertically or horizontally
s = zeros(60,10,3);
for x = 1:9
s(:,x,:) = x;
end
t = [s(:,:,1) , s(:,:,2), s(:,:,3)]; % hori
t = [s(:,:,1) ; s(:,:,2); s(:,:,3)]; % vert
Hope it helps, but this shows that there are multiple ways to achieve the same output in Matlab. Very powerful tool indeed.
A = rand(60, 10, 3);
B = reshape(A, [], size(A, 3));
should work for any dimensions of A.
Here is a solution that
stack[s] the 10 matrices with the dimension 60x3 next to each other in the first dimension.
a = rand(60,3,10);
% swap the 2nd and 3rd dimention
b = permute(a,[1,3,2]);
% condense to 2d
c = b(:,:);
% reshape x, y and z seperately so each group of 60 xs/ys/zs appends to the end of the previous group
x=reshape(c(:,1:3:end),[],1);
y=reshape(c(:,2:3:end),[],1);
z=reshape(c(:,3:3:end),[],1);
% condense to one 600*3 matrix
d = [x y z];

How can I optimize this integration code in matlab?

Using the Matlab Profiler I found that this line of code is creating a large bottleneck and slowing down my program. w,x,y,z are all 3D matrices containing the same dimensions (A x B x C) where A does not equal B and does not equal C. Is there any way to optimize this line of code to run faster?
dt = .5;
for t = 1: tstop
w(:,:,t+1)= sum( dt*(x(:,:,t:-1:1).*(y(:,:,1:t) - .002).*z(:,:,1:t)),3);
end
If you group some terms outside the for loop, you can get up to a 2x boost:
p = dt*(y - .002).*z;
for t = 1: tstop
w(:,:,t+1)= sum( x(:,:,t:-1:1).*p(:,:,1:t), 3);
end
It is now easier to notice that we are computing convolutions of x and p along the third dimension. If that dimension C (or tstop) is large, you can try to inline or optimize those convolutions.
I would reshape the 3D matrices into 2D ones, grouping the first 2 dimensions and keeping the time dimension as the second one. Then you can try to perform row-wise convolution with conv2 (if possible, as claimed in this answer), of fft. Find below a solution with fft (and zero-padding), assuming tstop = C:
X = reshape(x, [A*B, C]); % reshape to 2D
Y = reshape(y, [A*B, C]);
Z = reshape(z, [A*B, C]);
P = dt*(Y - .002).*Z; % grouped terms
z__ = zeros(A*B, C); % zero-padding
W = real(ifft(fft([z__, X]').*fft([z__, P]'))'); % column-wise fft
W = [zeros(A*B, 1), W(:, 1:C)]; % first half
w = reshape(W, [A, B, C+1]);
The results are the same, and depending of A,B,C, this can give you a big performance boost. Example with A=13, B=14, C=1155:
original: 1.026312 seconds
grouping terms: 0.509862 seconds
FFT: 0.033699 seconds

Sum over blocks in a 3D matrix - MATLAB

For a 3N by 3N by 3N matrix A, I would like to derive a N by N by N matrix B whose entries come from summation over blocks in A.
For example, B(1,1,1) = sum of all elements of A(1:3,1:3,1:3).
Basically, A is kind of a high resolution matrix and B is a low resolution matrix from summing over entries in A.
If memory is not a concern, you can use a "labelling" approach: build a 3-component label to group the elements of A, and use that label as the first input argument to accumarray to do the sum. The label uses integers from 1 to N, so the result of accumarray already has the desired shape (NxNxN).
N = 5;
F = 3; %// block size per dimension
A = rand(15,15,15); %// example data. Size FN x FN x FN
[ii jj kk] = ind2sub(size(A), 1:numel(A));
label = ceil([ii.' jj.' kk.']/F);
result = accumarray(label, A(:));
reshape + sum based approach and as such has to be pretty efficient -
sumrows = sum(reshape(A,3,[]),1); %// Sum along rows
sumcols = sum(reshape(sumrows,N,3,[]),2); %// Sum along cols
B = reshape(sum(reshape(sumcols,N*N,3,[]),2),N,N,N); %// Sum along 3rd dim
If you are crazy about one-liners, here's that combining all steps into one -
B = reshape(sum(reshape(sum(reshape(sum(reshape(A,3,[]),1),N,3,[]),2),N*N,3,[]),2),N,N,N);
For a 2D matrix, this would work:
B = reshape(sum(im2col(A, [3 3], 'distinct')), [N N]);
NB: You need the image processing toolbox.
But for 3D matrices, I don't know of any built-in function equivalent to im2col. You might have to use a loop. Left as an exercise to the reader ;)

Evaluate all pairs of matrices

I have a function f which takes 2 matrices with the same number of rows and procudes a scalar value. A am now looking for a possibility to create a new function which takes two lists of matrices and calls f for all pairs.
I need a moore efficient implementation of this loop:
% X = cell of matrices
% Y = cell of matrices
for k=1:length(X)
for l=1:length(Y)
M(k,l) = f(X{k},Y{l});
end
end
(It is not a requirement that X and Y are cells)
For example f could be the mean of squared distances
f = #(X,Y) mean(mean(bsxfun(#plus,dot(X,X,1)',dot(Y,Y,1))-2*(X'*Y)));
but don't question f, it is just an example of a much more complicated problem.
Firstly you should be pre-allocating M. Then you can cut out one loop quite easily by using meshgrid to generate a list of all pairs of elements in X and Y:
[K, L] = meshgrid(1:length(X), 1:length(Y));
M = zeros(size(K)); %//This preallocation alone should give you a significant speed up
for ii = 1:numel(K)
M(ii) = f(X{K(ii)},Y{L(ii)});
end
However, if you can implement f so that it is properly vectorized, you might be able to pass it two 3D matrices, i.e. the entire list of X and Y pairs and do it without a loop at all. But this depends entirely on what it is that your f does.
On possible solution that works is using cellfun
fy = #(x,Y) cellfun(#(y) f(x,y), Y);
fx = #(X,Y) cellfun(#(x) fy(x,Y), X, 'UniformOutput', false);
newF = #(X,Y) cell2mat(fx(X,Y)')';
however I suppose there is a better way to do this.