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.
87 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (2)
Matt J
el 26 de Oct. de 2017
Editada: Matt J
el 26 de Oct. de 2017
Change ans=c to y=c.
2 comentarios
Cam Salzberger
el 6 de Nov. de 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
Mohammed Hamaidi
el 19 de En. de 2022
Hello
Just write:
c=sum(a.*b)
2 comentarios
Matt J
el 19 de En. de 2022
If we were trying to avoid loops, it would be faster to do
c=a(:).'*b(:);
Samuel Gray
el 15 de Feb. de 2022
Editada: Samuel Gray
el 15 de Feb. de 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
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!