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 CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by