reduce matrix by sum of elements in Matlab [duplicate] - matlab

This question already has answers here:
Sum over blocks in a 2D matrix - MATLAB
(4 answers)
Closed 6 years ago.
I'm sure this is basic, but I can't come up to an easy way to reduce a matrix of population count by grid cell by adding up all the elements of the "small" cells which would fit in the new ones.
My matrix is 720x360 and would like to change it to 360x180. I thought about using imresize with 0.5 scale, and then assume the value per new grid is 4 times the old one.
I fell uneasy doing so though, as I can't find examples of similar cases. It'll be nice to have "sum" or "average" as an "interpolation" option or something like that.
Thanks in advance for any help
Cheers
Maria

If you have the Image Processing Toolbox:
x = randi(10,6,8); % example matrix
bs = [2 2]; % block size
y = col2im(sum(im2col(x, bs, 'distinct'), 1), [1 1], size(x)./bs, 'distinct');
How this works:
im2col(... 'distinct') arranges each distinct block of the given size into a column. sum(...,1) then sums each column. Finally, im2col(..., 'distinct') arranges the results back into a matrix of reduced size.

Related

Multiplication of each slice of a 3D matrix with 2D matrix [duplicate]

This question already has answers here:
Multiply a 3D matrix with a 2D matrix
(10 answers)
Closed 5 years ago.
I try to use svd in my work which will return the vector as below.
A=rand(10,5);
b=rand(10,5,100);
[U, S , V]=svd(A);
What I'm trying to do is to multiply each slice of b with U. With one specific slice, this operation is valid:
c=U'*b(:,:,1);
However when I try to use a vectorized method as below, it return array dimensions mismatch error.
Utran=U.';
c = cellfun(#(x) x.*b,num2cell(Utran,[1 2]),'UniformOutput',false);
I could probably use loop for the first method, but it's not efficient if I have a large matrix. Any idea what's my mistake here?
The following is a vectorized solution.
Not sure if it's faster than using loops; and even if it is, note that it uses more memorry for intermediate computations:
result = permute(sum(permute(conj(U), [1 3 4 2]).*b), [4 2 3 1]);

Extracting sub-matrices from a matrix [duplicate]

This question already has answers here:
How to divide an image into blocks in MATLAB?
(5 answers)
Closed 8 years ago.
I was wondering: I have a 100x100 matrix. I would like to split it in several 10x10 sub-matrices the first including columns and rows 1-10, then second including columns 11-20 and rows 1-10 and son on until eventually I have a set of 10x10 matrices.
Is there any way of doing this without needing to build an extremely complex array of for loops?
Thanks :)
If you need to extract the sub-matrices explicitly than mat2cell would be a reasonable choice:
sm = mat2cell( M, 10*ones(1,size(M,1)/10), 10*ones(1,size(M,2)/10) );
However, if you only need these submatrices for a local processing you can use blockproc
blockproc( M, [10 10], #myFun );

Find the closest weight vector to each instance in the data matrix

Suppose I have a weight matrix W nxm where m is the number of variables and the n is the number of instances. Also I have data matrix X of the same size. I try to find the closest weight vector to each instance in X. However both matrices are so dimensional therefore plain methods are not sufficient enough. I have tried some GPU trick at MATLAB but it does not work well since it was sequential approach that was calculating the closest weight for each instance sequentially. I am now looking for efficient one shot code. That takes all the W and X and find the winner with some MATLAB tricks with possibly some GPU addition. Is there any one that can suggest any code snippet in the MATLAB?
This is the thing that I wrote for sequential
x_in_d = gpuArray(x_in); % take input instance to device
W_d = gpuArray(W); % take weight matrix to device
Dx = W_d - x_in_d(ones(size(W_d,1),1),logical(ones(1,length(x_in_d))));
[d_min,winner] = min(sum((Dx.^2)'));
d_min = gather(d_min); %gather results
winner = gather(winner);
What do you mean by so dimensional? It's just an m x n matrix right?
It would be really helpful if you could provide some sample data, based off your description (which isn't the clearest), here is what I think your data looks like.
weights=
[1 4 2
5 3 1]
data=
[2 5 1
1 2 2]
And you want to figure out which row of weights is closest to the row of data? Which in this case would be the first row of weights for both rows of data.
Please edit your question to clarify what your asking for and consider using some examples.
EDIT:
I like Rody's Dup. Comment, if I am correct, check out: Link Here

applying norm function to rows of matrix - Matlab [duplicate]

This question already has answers here:
Vector norm of an array of vectors in MATLAB
(4 answers)
Closed 5 years ago.
I have a 3 columns, n rows matrix:
[ a,b,c;
d,e,f;
g,h,i; ]
I want to apply the norm function to each of the rows, and get a 1xn matrix containing the norms:
[ norm([a,b,c]);
norm([d,e,f]);
norm([g,h,i]); ]
I could do this with a for-loop, but is there a better way?
What about
norms = sqrt(sum(A.^2,1))
or
norms = sqrt(sum(A.^2,2))?
depending on whether your coordinates are in rows or in columns.
If readability is a bigger consideration than performance you might also consider:
norms = cellfun(#norm,num2cell(A,2));
This pattern is also adaptable to other operations along one dimension you might want to perform where MATLAB doesn't support it natively.
if the first dimension is not too large:
norms = sqrt(diag(A * A'));

Quick question about MATLAB elementwise division [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
How do I divide the rows of a matrix by different values in MATLAB (array division)
I have a matrix A (size MxN) in Matlab and a Vector b with M rows and now I want to divide all elemtens in the i-th row of A by the i-th entry in b like a(i,:)/b(i) but I really don't want to use this sort since I than use a for-loop and I definitly need a FAST solution!
Could anybody help out? Thanks!
Edit: Somehow I just came up with it after posting... My solution is bsxfun(#rdivide, [1 1; 2 2; 3 3], [2 2 6]'), do you think that's a good and fast one?
You want to use bsxfunc :
bsxfun(#rdivide,A,B)
http://www.mathworks.com/help/techdoc/ref/bsxfun.html