how to do multiplication for 3d array

6 views (last 30 days)
terrey
terrey on 7 Aug 2016
Commented: Walter Roberson on 8 Aug 2016
I want to do a multiplication procedure at the 12th line (Eig_faces = C*sorted_eig_mat;) for the below code:
B = cat(3, mydata{:});
meanface = mean(B,3);
C = B - repmat(meanface, [1 1 6]); %shifted_image(centering)
D = cov(B(1:end,:)); %cov matrix
[eig_mat, eig_val] = eig(D(:,:,1))
eig_val_vec = diag(eig_val);
[sorted_eig_val, eig_indices] = sort(eig_val_vec, 'descend');
sorted_eig_mat = zeros(6);
for i=1:6
sorted_eig_mat(:, i) = eig_mat(:,eig_indices(i)); %sorted eigen matrix
end
Eig_faces = C*sorted_eig_mat; %find out eigen faces
size_Eig_faces = size(Eig_faces);
The problem is C is a vectorized value that i got based on earlier concatenate process, therefore the multiplication can't be done since the dimension is different. I got an error: error using ==> mtimes.Input arguments must be 2-D. Anyone can help me? Thank you.
  2 Comments
the cyclist
the cyclist on 7 Aug 2016
What is the size of mydata{:}?
FYI, adding line numbers makes your code harder to work with. We to strip them out again to work with it.
terrey
terrey on 8 Aug 2016
i just try with a small one first, the size of mydata{:} is [2 3]

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 8 Aug 2016
If you happened to be working with a cpuarray you could use pagefun . But since you are not, you can use arrayfun
results_cell = arrayfun( @(pane) C(:,:,pane) * sorted_eig_mat, 1:size(C,3), 'Uniform', 0);
results_array = cat(3, results_cell{:});
  6 Comments
terrey
terrey on 8 Aug 2016
i think i need 6 matrices of 9 x 1 because i concatenate along the third dimension sir.
Walter Roberson
Walter Roberson on 8 Aug 2016
You have
sorted_eig_mat = zeros(6);
and then you fill in columns of sorted_eig_mat without changing the size of sorted_eig_mat. So your sorted_eig_mat is 6 x 6. In order to be able to do a matrix multiplication of A*B with B being 6 x 6, your A would have to be something by 6 and the result would be (the same) something by 6. You cannot get out a 9 x 1 matrix by multiplying anything by a 6 x 6.
I suggest that for now you do not try to be fancy, and instead just code a plain "for" loop to do the multiplications. That might involve using squeeze() to eliminate an internal singular dimension after you extract a particular sub-matrix from C.

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices 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!