how to solve array dimension for matrix multiplication issue?
5 views (last 30 days)
Anak Agung Adhi Dermawan on 9 Aug 2022
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?
Abderrahim. B on 9 Aug 2022
Edited: Abderrahim. B on 9 Aug 2022
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.
vec1 = randi(10, 366, 1) ;
vec2 = randi(10, 348, 1) ;
% zero padding
vec2 =[vec2 ; zeros((length(vec1) - length(vec2)), 1)] ;
vec = vec1*transpose(vec2) ;
% Element wise multiplication
vec = vec1.*vec2 ;
There is an in-build function in MATLAB called pad that you can also use.
Hope this helps