Matrix manipulation and matrix dimensions

11 views (last 30 days)
Amine Ben Ayara
Amine Ben Ayara on 13 Dec 2016
Edited: per isakson on 22 Dec 2017
Hello Matlab community again,
I am still trying to figure out few steps with getting from one dimension to another in matlab matrices.
So here is what I have now, Two matrices; A(73400 by 30), the number of rows here(73400) is equal to 14680 of stacked (5*1) matrices/arrays, hence 14680*5=73400.
The second matrix B(14680,5)
My objective is to compute the sum of the product of the transpose of each (5*1) that is stacked in A & each row from B . So the sum of the product should yield one element in each column (1 through 30).
The final matrix C is then (14680 by 30)
I tired to use two loops:
for i=1:30;
for j=1:14680
C(i,j)= sum(A(j:n,i)'*B(j,:));
end
end
It did not work. But in words, every iteration from 1 through 30, pick up a (5*1) from A, transpose it so it is same dimensions as rows from B, then compute sum of the product of each row from (1:14680).
Matrix B is a constant input, so it doesn't change. The only change is the values from each column in A ( from 1 through 30).
I hope I explained this correctly this time.
Thank you so much guys!
Amine

Answers (2)

David Goodmanson
David Goodmanson on 13 Dec 2016
Edited: David Goodmanson on 13 Dec 2016
Hello Amine, This is only an answer if the following idea is correct: For a given 5x1 (call it y) and if B is mx5, I think you are looking for the number
sum{k = 1 to m} sum((y.').*B(k,:)) which is the same as sum(B*y).
If not, you can ignore what's next. Anyway, the following seems to work in this case:
% A = m*5 x q
% B = m x 5
D = reshape(A,5,m*q) % line up 5x1's side by side
E = B*D % all possible dot products of a
% transposed 5x1 from A and a row of B
F = sum(E) % sum down rows of B
C = reshape(F,m,q) % place the elements
or in cryptic form
C = reshape(sum(B*reshape(A,5,m*q)),m,q)
  2 Comments
Amine Ben Ayara
Amine Ben Ayara on 13 Dec 2016
Edited: per isakson on 22 Dec 2017
Good morning David, Thank you so much for taking the time to help me with this. So I used the second portion of your script, assuming that the results will be all in matrix form that I can understand better, This is What I did: My matrix A(73400 by 30) and the 73400 rows are the (5by1) stacked up or better yet, that I have 14680 sets of (5by1) (14680*5=73400) and the matrix B(14680 by 5).
-------------Based on your script: ----- I gussed m=14680 and q=30
D = reshape(A,5,14680*30); % line up 5x1's side by side
E = B*D; % all possible dot products of a
% transposed 5x1 from A and a row of B
F = sum(E) ; % sum down rows of B
C = reshape(F,14680,5); % place the elements
I got an error message when I executed D = reshape(A,5,14680*30 >> D = reshape(A,5,14680*30); Error using reshape To RESHAPE the number of elements must not change. I would greatly appreciate any feedback on this, please! Kind Regards, Amine
David Goodmanson
David Goodmanson on 13 Dec 2016
Hello Amine, yes m = 14680, q = 30. If matrix A is 2d of size 73400 x 30, the reshape command for D pretty much has to work. I see from your comment to Jan's answer that you have extracted A from a 4d matrix. Could you check the size of A just before you try the reshape command? Thanks.

Sign in to comment.


Jan
Jan on 13 Dec 2016
A = rand(73400, 30);
B = rand(14680, 5);
AA = reshape(A, 5, 14680 * 30);
C = reshape(B * AA, 14680, 14680, 30);
Result = sum(CC, 3);
Do you mean something like this? If not, please explain the wanted result again. What is the desired size? A small example might be useful also.
  4 Comments
Amine Ben Ayara
Amine Ben Ayara on 13 Dec 2016
Here is an Example. My first matrix A (73400 by 30) ( I must mention that it was actually ( 5 by 5 by 14680 by 30) so 4D , but I converted it to 2D because I only need the first column (5*1) from each (5*5) matrix (14680 of them) in each set (30), so now and I stacked the (5*1) columns so the total rows became =14680*5=73400 and total number of columns is 30, hence 2D (73400*30). Second matrix is B(14680,5) My objective is to do the sum of the product of the transpose of each (5*1) from matrix A to each row from matrix B. The final product/Matrix should have this dimension (14680*30)
Jan
Jan on 16 Dec 2016
@Amine: Your descriptions are hard to read. I cannot follow you. Can you provide real values? Perhaps a 2x3 input instead of 73400x30 would clarify already, what you want to achieve. The error message shows, that you need a lot of RAM to claculate such large inputs efficiently. I do not know, if your machine has 120GB or only 4GB.

Sign in to comment.

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!