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.

조회 수: 237 (최근 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

답변 (2개)

Matt J
Matt J 2017년 10월 26일
편집: Matt J 2017년 10월 26일
Change ans=c to y=c.
  댓글 수: 2
viswaja Katragadda
viswaja Katragadda 2017년 10월 26일
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 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
Mohammed Hamaidi 2022년 1월 19일
Hello
Just write:
c=sum(a.*b)
  댓글 수: 2
Samuel Gray
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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by