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.

168 views (last 30 days)
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

Answers (2)

Matt J
Matt J on 26 Oct 2017
Edited: Matt J on 26 Oct 2017
Change ans=c to y=c.
  2 Comments
Cam Salzberger
Cam Salzberger on 6 Nov 2017
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

Sign in to comment.


Mohammed Hamaidi
Mohammed Hamaidi on 19 Jan 2022
Hello
Just write:
c=sum(a.*b)
  2 Comments
Samuel Gray
Samuel Gray on 15 Feb 2022
Edited: Samuel Gray on 15 Feb 2022
» 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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by