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.

214 visualizzazioni (ultimi 30 giorni)
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

Risposte (2)

Matt J
Matt J il 26 Ott 2017
Modificato: Matt J il 26 Ott 2017
Change ans=c to y=c.
  2 Commenti
viswaja Katragadda
viswaja Katragadda il 26 Ott 2017
Hello, Thank you for your answer. I just wanted to ask you how can you make sure that the size of the two vectors and the size of the matrix are the same?
Cam Salzberger
Cam Salzberger il 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

Accedi per commentare.


Mohammed Hamaidi
Mohammed Hamaidi il 19 Gen 2022
Hello
Just write:
c=sum(a.*b)
  2 Commenti
Samuel Gray
Samuel Gray il 15 Feb 2022
Modificato: Samuel Gray il 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

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by