how to solve array dimension for matrix multiplication issue?

7 ビュー (過去 30 日間)
Anak Agung Adhi Dermawan
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 件のコメント
Walter Roberson
Walter Roberson 2022 年 8 月 9 日
What size of result do you want?
Anak Agung Adhi Dermawan
Anak Agung Adhi Dermawan 2022 年 8 月 9 日
366 x 1

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

採用された回答

Abderrahim. B
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)
ans = 1×2
366 1
size(vec2)
ans = 1×2
366 1
% Multiplication
vec = vec1*transpose(vec2) ;
size(vec)
ans = 1×2
366 366
% Element wise multiplication
vec = vec1.*vec2 ;
size(vec)
ans = 1×2
366 1
There is an in-build function in MATLAB called pad that you can also use.
Hope this helps
  3 件のコメント
Abderrahim. B
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)
Anak Agung Adhi Dermawan
Anak Agung Adhi Dermawan 2022 年 8 月 9 日
thank you sir

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by