Dot Product function errors

1 次查看(过去 30 天)
Andrew Cruce
Andrew Cruce 2017-10-5
I can't seem to get the dot product function to work properly. When I try something like:
a = [1 2 3]
b = [4 5 6]
c = dot(a,b)
I get "Index exceeds matrix dimensions
if I try
a = [1 1 1]
c = dot (a,a)
I get
20 20 20
20 20 20
20 20 20
if I try
a = [1 2 3]
c= dot(a,a)
I get Index exceeds matrix dimensions
I've been looking at this code for an hour and can't see where it differs from the explanation of the dot produce.
Cross product works properly

回答(1 个)

James Tursa
James Tursa 2017-10-5
编辑:James Tursa 2017-10-5
You have inadvertently created a variable named "dot" that is shadowing the MATLAB function named "dot". Clear that variable named "dot", and in the future do not use that name anymore for variables. Similarly, don't create variables named "cross", "sum", etc. that can shadow MATLAB functions. E.g.,
>> dot = 20;
>> which dot
dot is a variable.
>> a = [1 2 3];
>> c = dot(a,a)
??? Index exceeds matrix dimensions.
>> a = [1 1 1];
>> c = dot(a,a)
c =
20 20 20
20 20 20
20 20 20
>> clear dot
>> which dot
C:\Program Files (x86)\MATLAB\R2011a\toolbox\matlab\specfun\dot.m
>> c = dot(a,a)
c =
3
  1 个评论
Andrew Cruce
Andrew Cruce 2017-10-6
Thank you very much for the quick response. I'm new to MatLab and didn't realize what I had done.
Andy Cruce

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by