-1

I have A matrix which is 16x16x155460. I have a B vector which is 12955x1. I want to multiply each 1:16x1:16x1+12*n:12+12*nwith the elements of B(n). So my goal is to find the weighted sum of the A according to B. My way to do this as follows (I don't want to use for-loop and my method gives wrong answer, I could not obtain the 1:12 vectors which is consecutive) :

B = repmat(B,[1 16 16]);
B = permute(B,[2 3 1]);
B = repmat(B,[1 1 12]);
result = B.*(A);

As a small example n=2 :

A(:,:,1)=[1 2; 3 4]
A(:,:,2)=[1 2; 3 4]
A(:,:,3)=[1 2; 3 4]
A(:,:,4)=[1 2; 3 4]
B      = [2,3]

Result would be:

result(:,:,1)=A(:,:,1)*B(1);
result(:,:,2)=A(:,:,2)*B(1);
result(:,:,3)=A(:,:,1)*B(2);
result(:,:,4)=A(:,:,2)*B(2);
Divakar
  • 218,885
  • 19
  • 262
  • 358
toygan kılıç
  • 422
  • 4
  • 16
  • What would be the output array size? – Divakar Oct 22 '15 at 19:57
  • I would reshape the matrix to `16x16x12x12955` first... – beaker Oct 22 '15 at 20:05
  • Do I understand you right, that you use only `A(16,16,:)` and non of the other data in A? Your formula `1+12*n:12+12*n` is wrong, it does not start with 0 and exceeds the length of A – Daniel Oct 22 '15 at 20:14
  • That example just confused me :( – beaker Oct 22 '15 at 20:19
  • Pitfalls of over-simplified examples really @beaker – Divakar Oct 22 '15 at 20:22
  • @Divakar But it doesn't even use all of the planes of `A` – beaker Oct 22 '15 at 20:24
  • @beaker Yup, that's another issue here. – Divakar Oct 22 '15 at 20:24
  • And are the elements of `B` repeated individually like `1 1 1 1 2 2 2 2` or as a vector like `1 2 1 2 1 2 1 2`? (assuming `B = [1 2]`) – beaker Oct 22 '15 at 20:26
  • Think about dividing the 16x16x12955 matrix to new matrix which has 12 pages 1:16x1:16x1:12 (All of this values would be multiplied by first element of the vector) 1:16x1:16x13:23 (All of this values would be multiplied by second element of the vector) and goes like this. So we have 12 images which are 16x16 and they would be multiplied by a constant for considering weighted mean. I'm sorry to get confused you :( – toygan kılıç Oct 22 '15 at 20:44
  • .. and the output array size would be ...? – Divakar Oct 22 '15 at 20:50
  • 16x16x155460 is true value and the result would be 16x16x155460 matrix again the only difference would be multipication with vector values. In the comment I wrote wrong number. – toygan kılıç Oct 22 '15 at 21:28

1 Answers1

2

If I understood the problem correctly, you can use the powerful trio of bsxfun, permute and reshape to solve it, like so -

[M,N,R] = size(A);
mult_out = bsxfun(@times,reshape(A,M,N,numel(B),[]),permute(B(:),[4 3 1 2]))
out = reshape(mult_out,M,N,[])
Divakar
  • 218,885
  • 19
  • 262
  • 358
  • why are you using permute for B, it is a vector? – toygan kılıç Oct 23 '15 at 07:49
  • @toygankılıç permute on `B` just "shifts" all its elements to the third dimension, which is what you want for expanded elementwise multiplication with `A`. Read more about how permute works , here - http://www.mathworks.com/help/matlab/ref/permute.html – Divakar Oct 23 '15 at 08:00
  • Thanks a lot for your efficient solutions! I know it is stupid question but matrix dimensions could be problem sometimes. Is there a difference between [4 3 1 2] and [3 4 1 2]? – toygan kılıç Oct 23 '15 at 08:28
  • @toygankılıç Nope, both would be the same. As long as you keep `1` at the third position, dimension IDs at other three places don't matter at all. – Divakar Oct 23 '15 at 08:31
  • How could you feel which dimension will be important for multipication? – toygan kılıç Oct 23 '15 at 08:45
  • @toygankılıç Not sure what you mean by "important". But, let's say you meant for this particular problem, which dimension is "required" instead of "important" for multiplication, that would be third dim in this case, as third dim of `A` elements are to be multiplied by `B`. If by "important", instead you meant which dimensions are to be used at places other than the third dimension, that's purely random, but I like to go from higher to lower. See [`here`](http://stackoverflow.com/a/26187897/3293881) for a related higher dimensional permute example. – Divakar Oct 23 '15 at 08:51