how to solve array dimension for matrix multiplication issue?
조회 수: 4 (최근 30일)
이전 댓글 표시
Anak Agung Adhi Dermawan
2022년 8월 9일
댓글: Anak Agung Adhi Dermawan
2022년 8월 9일
Dear matlab expert, I have 2 variables which 1 has 366 x 1 and the other one has 348 x 1 matrix dimension, I want to multiply these 2 variables but I got an error because incompatible array sizes for this operation. how can I fix this?
댓글 수: 2
채택된 답변
Abderrahim. B
2022년 8월 9일
편집: Abderrahim. B
2022년 8월 9일
Hi !
Yes, multiplication of two arrays with incompatible sizes will throw an error !
A possible solution is to pad (zero pad is the common method) the array that has smaller size.
Demo:
vec1 = randi(10, 366, 1) ;
vec2 = randi(10, 348, 1) ;
% zero padding
vec2 =[vec2 ; zeros((length(vec1) - length(vec2)), 1)] ;
size(vec1)
size(vec2)
% Multiplication
vec = vec1*transpose(vec2) ;
size(vec)
% Element wise multiplication
vec = vec1.*vec2 ;
size(vec)
Hope this helps
댓글 수: 3
Abderrahim. B
2022년 8월 9일
vec1 = randi(10, 366, 1) ;
vec2 = randi(10, 348, 1) ;
% zero padding
value = mean(vec2) ; % set value to whatever you want, I set it to the average of the vector.
vec2 =[vec2 ; value*ones((length(vec1) - length(vec2)), 1)] ;
% Multiplication
vec = vec1*transpose(vec2) ;
size(vec)
% Element wise multiplication
vec = vec1.*vec2 ;
size(vec)
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!