Calculating the inner product of two input vectors and a matrix using for loop and inner function. How do I change the code I have? Thank you so much for your answers.
90 次查看(过去 30 天)
显示 更早的评论
function y = inner(a,b);
% This is a MatLab function to compute the inner product of
% two vectors a and b.
% Call syntax: y = inner(a,b) or inner(a,b)
% Input: The two vectors a and b
% Output: The value of the inner product of a and b.
c=0; % intialize the variable c
n= length(a); % get the lenght of the vector a
for k=1:n % start the loop
c=c+a(k)*b(k); % update c by the k-th product in inner product
end % end loop
ans = c % print value of c = inner product
0 个评论
回答(2 个)
Matt J
2017-10-26
编辑:Matt J
2017-10-26
Change ans=c to y=c.
2 个评论
Cam Salzberger
2017-11-6
validateattributes is a nice way of checking input. It will produce a reasonable error message if there is something wrong with the input. Try something like:
validateattributes(a, {'numeric'}, {'size', size(b)}, mfilename)
If you don't want an error message to occur, and would prefer to just react to it, you can use:
if ~isequal(size(a), size(b))
...react to unequal sizes here...
end
-Cam
Mohammed Hamaidi
2022-1-19
Hello
Just write:
c=sum(a.*b)
2 个评论
Matt J
2022-1-19
If we were trying to avoid loops, it would be faster to do
c=a(:).'*b(:);
Samuel Gray
2022-2-15
编辑:Samuel Gray
2022-2-15
» innerprod(data1,data1)
ans =
1.7321e+11
» innerprod(data1,data1,1)
ans =
1.7321e+11
» innerprod(data1,data1,2)
MTIMES (*) is not fully supported for integer classes. At least one argument must be scalar.
Error in innerprod (line 8)
y=a(:).'*b(:);
...so fine let's add a line to convert to double if the input data is integer...
» innerprod_tester(data1)
[innerprod] tconvsec=0.2
[innerprod] method: 1 tclcsec: 0.2
[innerprod] tconvsec=0.3
[innerprod] method: 1 tclcsec: 0.2
[innerprod] tconvsec=0.2
[innerprod] method: 1 tclcsec: 0.2
[innerprod] tconvsec=0.3
[innerprod] method: 1 tclcsec: 0.2
[innerprod] tconvsec=0.2
[innerprod] method: 1 tclcsec: 0.2
[innerprod] tconvsec=0.2
[innerprod] method: 2 tclcsec: 0.1
[innerprod] tconvsec=0.2
[innerprod] method: 2 tclcsec: 0.1
[innerprod] tconvsec=0.2
[innerprod] method: 2 tclcsec: 0.1
[innerprod] tconvsec=0.4
[innerprod] method: 2 tclcsec: 0.1
[innerprod] tconvsec=0.2
[innerprod] method: 2 tclcsec: 0.1
[innerproc_tester]: dtsecavg: 0.5, 0.4
[innerproc_tester]: dtsecvar: 0.0, 0.0
the 2nd method is faster but it requires double data..well at least FP data.
Probably the results below would be faster with singles. But as we value precision over accuracy ;), we prefer doubles to singles, even if the results are unrealistic, off by orders of magnitude.
Notling like being both slow and in the wrong zip-code...
The first method will work wtih integers and as you can see it takes more time to cast the integers to double than to do the calculation. In this case "data1" is a 100M list of int16 values.
That's a lot of 0s to carry around unnecessarily!
So I made the cast an elective depending on the method and...
[innerprod] method: 1 tclcsec: 0.0
[innerprod] method: 1 tclcsec: 0.0
[innerprod] method: 1 tclcsec: 0.0
[innerprod] method: 1 tclcsec: 0.1
[innerprod] method: 1 tclcsec: 0.0
[innerprod] tconvsec=0.2
[innerprod] method: 2 tclcsec: 0.1
[innerprod] tconvsec=0.3
[innerprod] method: 2 tclcsec: 0.1
[innerprod] tconvsec=0.2
[innerprod] method: 2 tclcsec: 0.1
[innerprod] tconvsec=0.2
[innerprod] method: 2 tclcsec: 0.1
[innerprod] tconvsec=0.3
[innerprod] method: 2 tclcsec: 0.1
[innerproc_tester]: dtsecavg: 0.0, 0.4
[innerproc_tester]: dtsecvar: 0.0, 0.0
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!