Does the function movmean in Matlab create an average of all dimensions of the matrix? - arrays

I am trying to smooth the temporal history of each pixel in my matrix- in other words, trying to smooth each pixel through both 'space' (mxn) and 'time'(third dimension). I am using the function movmean to create an average of each pixel in time of a 1000x1000x8 matrix.
I am currently using the following code to take an average, using a window size of 5, operating along the third dimension:
av_matrix = movmean(my_matrix,5,3)
This is creating an average as expected, but I'm wondering if the window is just operating in the mxn direction and not taking the average along the third dimension as well.

To compute a moving average along the n dimensions of an n-dimensional array (the "window" is an n-dimensional rectangle), the simplest way is to use convolution (see convn).
You need to be careful with edge effects, that is, when the convolution kernel (or n-dimensional window) partially slides out of the data. What movmean does is average over the actual data points only. To achieve that behaviour you can
compute the sum over the kernel via convolution with the 'same' option; and then
divide each entry by the number of actual data points from which it was computed. This number can also be obtaind via convolution, namely, applying the kernel to an array of ones.
So, all you need is:
my_matrix = randi(9,5,5,3); % example 3D array
sz = [3 3 2]; % 3D window size
av_matrix = convn(my_matrix, ones(sz), 'same') ... % step 1
./convn(ones(size(my_matrix)), ones(sz), 'same'); % step 2
Check:
The following examples use
>> my_matrix
my_matrix(:,:,1) =
6 8 2 1 8
4 6 7 9 8
4 5 1 4 3
5 5 8 7 9
3 6 6 4 9
my_matrix(:,:,2) =
8 8 5 3 6
8 9 6 9 1
9 5 6 2 2
1 7 4 1 2
5 4 7 4 9
my_matrix(:,:,3) =
6 5 8 6 6
1 6 8 6 1
5 5 1 6 7
1 1 2 9 8
1 2 6 1 2
With edge effects:
>> mean(mean(mean(my_matrix(1:2,1:2,1:2))))
ans =
7.125000000000000
>> av_matrix(1,1,1)
ans =
7.125000000000000
Without edge effects:
>> mean(mean(mean(my_matrix(1:3,1:3,1:2))))
ans =
5.944444444444445
>> av_matrix(2,2,1)
ans =
5.944444444444445

Related

Matrix transformation in MATLAB

For example, I have a matrix A (Figure 1). When the variable n = 2, I want it to be transformed to the matrix B. The red rectangle shows the transformation rule of every column. According to this rule, when the n = 3, it can become the matrix C.
I have written a script using a for loop method, but it is a waste of time when the matrix A is very large (e.g. 11688* 140000). Is there an efficient way to solve this problem?
Figure 1:
Here is a way using reshape and implicit expansion:
result = reshape(A((1:size(A,1)-n+1) + (0:n-1).', :), n, []);
For example assume that n = 3. Implicit expansion is used to extract indices of rows:
row_ind = (1:size(A,1)-n+1) + (0:n-1).';
The following matrix is created:
1 2
2 3
3 4
Extract the desired rows of A:
A_expanded = A(row_ind, :)
When the matrix row_ind is used as an index it behaves like a vector:
1
2
1 2 3
2 3 -> 2
3 4 3
4
A_expanded =
3 5 7
6 8 9
2 6 3
6 8 9
2 6 3
1 2 1
Now A_expanded can be reshaped to the desired size:
result = reshape(A_expanded, n, []);
>>result =
3 6 5 8 7 9
6 2 8 6 9 3
2 1 6 2 3 1
If you have the Image Processing Toolbox you can use im2col as follows:
result = im2col(A, [n 1], 'sliding');

MATLAB: how to pass in the diagonal of a matrix as an argument in another matrix?

Let c be 2D array, and x and y be 1D arrays of the same length (for instance, let's have x=1:7 and y=3:9).
I need to find a way to pass in arguments from x and y in the way I will describe below.
If I put simply c(x,y) it will give a 7 by 7 matrix. I don't want that.
Instead, I want to pass in the diagonal of the [x y] matrix: ((x(1), y(1)), (x(2), y(2))...(x(7), y(7)). Is there a way to do this without a for loop or any iterative statement?
You are looking for sub2ind function
res = c( sub2ind(size(c), x, y ) )
There's an easier way. If you're looking for a diagonal, use diag. If you have a matrix c:
c =
5 8 4 2 9 1 6 1 1
9 8 7 5 9 3 2 7 5
2 3 9 10 2 1 4 2 2
3 2 9 2 4 4 7 2 4
3 9 10 8 7 5 2 1 8
5 6 3 7 6 1 10 5 2
6 1 7 3 10 8 2 4 2
you can find the main diagonal by using diag with no extra arguments:
>> diag(c)
ans =
5
8
9
2
7
1
2
The second argument, though, indicates which diagonal you want as an offset from the main diagonal. So the default diagonal is equal to 0. If you want the diagonal starting at c(1,3), that's 2 above the main diagonal, so
>> diag(c,2)
ans =
4
5
2
4
2
5
2
Similarly, if you want the diagonal starting at c(4,1), the offset is -3:
>> diag(c,-3)
ans =
3
9
3
3

Extend a 2D-matrix to a 3D-matrix by multiplying with a vector [duplicate]

This question already has an answer here:
Multiply 2D Matrix with vector to span third dimension - MATLAB
(1 answer)
Closed 7 years ago.
Here what I would like to achieve:
I have a matrix C
C=[1 2 3; 4 5 6; 7 8 9];
And a vector a
a=[1 2];
I would like to make such an operation, that each element of the a vector is multiplied with C ( scalar multiplication ) and out comes a 3-dimensional array D:
(:,:,1) =
1 2 3
4 5 6
7 8 9
(:,:,2) =
2 4 6
8 10 12
14 16 18
It would certainly work with a loop, but, since I'll need this operation on multiple occasions, a oneliner would be a great saver.
This is a beautiful example for the use of bsxfun and reshape. While #thewaywewalks proposes first calling bsxfun and reshaping the result, I'd suggest the opposite. This makes one of the key concepts of bsxfun - the singleton dimension expansion - more clear:
out = bsxfun(#times,C,reshape(a,1,1,[]))
ans(:,:,1) =
1 2 3
4 5 6
7 8 9
ans(:,:,2) =
2 4 6
8 10 12
14 16 18
With reshape(a,1,1,[]), you make a be in the third dimension. If you now apply bsxfun, it will multiply the matrix C with each element of a.
Some reshape'ing and some bsxfun will do:
out = reshape(bsxfun(#mtimes, C(:), a(:).'), [size(C),numel(a)] )
As suggested in hbaderts answer one could also use bsxfun's capability of dimension expansion, and provide a permuted vector of factors:
out = bsxfun(#mtimes,C,permute(a,[3,1,2]))
out(:,:,1) =
1 2 3
4 5 6
7 8 9
out(:,:,2) =
2 4 6
8 10 12
14 16 18
I have another method for the benchmark compare...
IMO it's the neatest way, at least for the syntax/readability term:
out = reshape(kron(a,C),[size(C),numel(a)]);
out(:,:,1) =
1 2 3
4 5 6
7 8 9
out(:,:,2) =
2 4 6
8 10 12
14 16 18
Another possibility is to use matrix multiplication of C as a column vector times a as a row vector (this gives all element-wise products), and then reshape the result:
out = reshape(C(:)*a, size(C,1), size(C,2), numel(a));
EDIT (BENCHMARKING): Since several solutions (including mine below) have been suggested, here is some rough benchmarking to compare the different solutions, using larger arrays:
a=1:10;
N=1000; timers=zeros(N,6);
for ii=1:N; C=rand(400);
tic; out = repmat(C,[1,1,numel(a)]).*reshape(repelem(a,size(C,1),size(C,2)),[size(C),numel(a)]); timers(ii,1)=toc;
tic; out = bsxfun(#times,C,reshape(a,1,1,[])); timers(ii,2)=toc;
tic; out = reshape(C(:)*a, size(C,1), size(C,2), numel(a)); timers(ii,3)=toc;
tic; out = bsxfun(#mtimes,C,permute(a,[3,1,2])); timers(ii,4)=toc;
tic; out = reshape(bsxfun(#mtimes, C(:), a(:).'), [size(C),numel(a)] ); timers(ii,5)=toc;
tic; out = reshape(kron(a,C),[size(C),numel(a)]); timers(ii,6)=toc;
end;
mean(timers)
ans =
0.0080863 0.0032406 0.0041718 0.015166 0.0074462 0.0033051
... suggesting that #hbaderts solution is fastest, then #Adiel's, then #Luis Mendo's, then #thewaywewalk's (1), then mine, then #thewaywewalk's (2).
My solution:
Another option, using repmat and reshape (no bsxfun):
out = repmat(C,[1,1,numel(a)]).*reshape(repelem(a,size(C,1),size(C,2)),[size(C),numel(a)])
out(:,:,1) =
1 2 3
4 5 6
7 8 9
out(:,:,2) =
2 4 6
8 10 12
14 16 18
This is the element-wise multiplication of two arrays. The first is your original matrix C repeated numel(a) times in the third dimension:
repmat(C,[1,1,numel(a)])
ans(:,:,1) =
1 2 3
4 5 6
7 8 9
ans(:,:,2) =
1 2 3
4 5 6
7 8 9
The second is the same size as the first, with each slice containing the corresponding element of a:
reshape(repelem(a,size(C,1),size(C,2)),[size(C),numel(a)])
ans(:,:,1) =
1 1 1
1 1 1
1 1 1
ans(:,:,2) =
2 2 2
2 2 2
2 2 2

array manipulation with circshift octave / matlab

1) I'm trying to shift an array's (outer cells) first and last cell inward at the same time. Here's an inward animation of what I'm trying to do with the array
inward animation. As you can see the outer cells are moving inward (from the ends) at the same time
Here's a pic but the animation shows it much better please note the array could have either an even or odd amount of cells
Inner rotation steps
1 2 3 4 5 6 7
4 1 2 3 6 7 5
3 4 1 2 7 5 6
2) I'm trying to shift an array's middle cells outward using circshift (which I think is fastest) at the same time.
Here's an outward animation of what I'm trying to do also
outward animation. As you can see the middle of the signal is moving outward (left and right) at the same time.
Here's a pic but the animation shows it much better please note the array could have either an even or odd amount of cells
Outer rotation steps
1 2 3 4 5 6 7
2 3 4 1 7 5 6
3 4 1 2 6 5 7
Example: inward
a = (1:7)
y=circshift(A,[0 -2]) %shift end of array inward
3 4 5 6 7 1 2
a = (1:7)
y=circshift(A,[0 2]) %shift beginning of array inward
6 7 1 2 3 4 5
Not to sure how to do the middle cells shifting outward using circshift or the outer cells shifting inward at the same time
I'm not sure about how to start circshift from the center and move the array outwards / inwards to get this effect.
Please note I'm not trying to get this equation I'm just trying to get the arrays to move in the same way. I'm using octave 3.8.1 which is compatible with matlab.
A = 1:7;
split = ceil(numel(A)/2);
n = 2;
A(1:split) = circshift(A(1:split), [0, n]);
A(split+1:end) = circshift(A(split+1:end), [0, -n]);
Put the last three lines in a loop if you like. Also just change the signs of n for inwards or outwards
what about constructing new indices instead of using circshift:
A = 1:7;
halfLen = ceil(length(A)/2); % or use ceil to
idcsOutward = [2:halfLen,1,length(A),(halfLen+1):(length(A)-1)];
B1 = A(idcsOutward)
B2 = B1(idcsOutward)
% and inward:
idcsInward = [halfLen,1:(halfLen-1),(halfLen+2):length(A),halfLen+1];
C1 = A(idcsInward)
C2 = C1(idcsInward)
Result is:
B1 =
2 3 4 1 7 5 6
B2 =
3 4 1 2 6 7 5
C1 =
4 1 2 3 6 7 5
C2 =
3 4 1 2 7 5 6

Vectorized Reshaping of Columns in an Array

I have an array A, and want to reshape the last four elements of each column into a 2x2 matrix. I would like the results to be stored in a cell array B.
For example, given:
A = [1:6; 3:8; 5:10]';
I would like B to contain three 2x2 arrays, such that:
B{1} = [3, 5; 4, 6];
B{2} = [5, 7; 6, 8];
B{3} = [7, 9; 8, 10];
I can obviously do this in a for loop using something like reshape(A(end-3:end, ii), 2, 2) and looping over ii. Can anyone propose a vectorized method, perhaps using something similar to cellfun that can apply an operation repeatedly to columns of an array?
The way I do this is to look at the desired indices and then figure out a way to generate them, usually using some form of repmat. For example, if you want the last 4 items in each column, the (absolute) indices into A are going to be 3,4,5,6, then add the number of rows to that to move to the next column to get 9,10,11,12 and so on. So the problem becomes generating that matrix in terms of your number of rows, number of columns, and the number of elements you want from each column (I'll call it n, in your case n=4).
octave:1> A = [1:6; 3:8; 5:10]'
A =
1 3 5
2 4 6
3 5 7
4 6 8
5 7 9
6 8 10
octave:2> dim=size(A)
dim =
6 3
octave:3> n=4
n = 4
octave:4> x=repmat((dim(1)-n+1):dim(1),[dim(2),1])'
x =
3 3 3
4 4 4
5 5 5
6 6 6
octave:5> y=repmat((0:(dim(2)-1)),[n,1])
y =
0 1 2
0 1 2
0 1 2
0 1 2
octave:6> ii=x+dim(1)*y
ii =
3 9 15
4 10 16
5 11 17
6 12 18
octave:7> A(ii)
ans =
3 5 7
4 6 8
5 7 9
6 8 10
octave:8> B=reshape(A(ii),sqrt(n),sqrt(n),dim(2))
B =
ans(:,:,1) =
3 5
4 6
ans(:,:,2) =
5 7
6 8
ans(:,:,3) =
7 9
8 10
Depending on how you generate x and y, you can even do away with the multiplication, but I'll leave that to you. :D
IMO you don't need a cell array to store them either, a 3D matrix works just as well and you index into it the same way (but don't forget to squeeze it before you use it).
I gave a similar answer in this question.

Resources