how to do multiplication for 3d array

6 ビュー (過去 30 日間)
terrey
terrey 2016 年 8 月 7 日
コメント済み: Walter Roberson 2016 年 8 月 8 日
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 件のコメント
the cyclist
the cyclist 2016 年 8 月 7 日
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 2016 年 8 月 8 日
i just try with a small one first, the size of mydata{:} is [2 3]

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 8 月 8 日
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 件のコメント
terrey
terrey 2016 年 8 月 8 日
i think i need 6 matrices of 9 x 1 because i concatenate along the third dimension sir.
Walter Roberson
Walter Roberson 2016 年 8 月 8 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by