Matrix manipulation and matrix dimensions

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

Respuestas (2)

David Goodmanson
David Goodmanson el 13 de Dic. de 2016
Editada: David Goodmanson el 13 de Dic. de 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 comentarios

Amine Ben Ayara
Amine Ben Ayara el 13 de Dic. de 2016
Editada: per isakson el 22 de Dic. de 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 el 13 de Dic. de 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.

Iniciar sesión para comentar.

Jan
Jan el 13 de Dic. de 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 comentarios

Amine Ben Ayara
Amine Ben Ayara el 13 de Dic. de 2016
Good morning Jan, thank you for the reply, I really appreciate. So here is an example. The 2D matrix is A(73400 by 30) (total rows 73400=14680*5) [ 14680 of 5*1 arrays/matrices stacked horizontally, 73400=14680*5. The next matrix say; B(14680 by 5) ( where each row has a unique identifier, hence U need the operation to be exclusive between each row of B (i,:) with i=1:14680, and the transpose of each column(5*1) from A, which with transpose it becomes a row identical to that of B. First iteration suppose now, J=1:30 ( the 30 columns,2nd dimension from A), Every iteration j: Compute the sum of the product from transpose of the first column in A; the dimension of each array is then (1*5) and (1*5) so the result is only one element. then move down to the following row in B, and the next column in A (all the way through 14680, hence j=1:14680 and the result matrix will have the following dimension (14680 by 30) e.g: first 5 rows, in 1 st column from A is A11=A(1:,5,1)=[1;2;3;4;5]; First row from B is B1=B(1,:)=[1 1 1 1 1 1]; SumProduct= Sum(A11'.*B1)=15 ( this element will go in the final matrix in C(1,1), the next iteration will be for the following row in B(i=2) and the second set of 5 rows in A (6:10,1) ... until all the rows in the first column of A are done, then repeat the same process for the next column, hence (j=1:30, with j number of columns in A). I hope this helps? Sorry if It was long.
Amine Ben Ayara
Amine Ben Ayara el 13 de Dic. de 2016
Hello Jan, I just tried to run the script you provided and I got this error message: Error using * Requested 14680x440400 (48.2GB) array exceeds maximum array size preference. Creation of arrays greater than this limit may take a long time and cause MATLAB to become unresponsive. See array size limit or preference panel for more information.
Amine Ben Ayara
Amine Ben Ayara el 13 de Dic. de 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 el 16 de Dic. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Preguntada:

el 13 de Dic. de 2016

Editada:

el 22 de Dic. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by