MATLab matrix multiplier not outputting the correct product..

조회 수: 1 (최근 30일)
Ryan
Ryan 2014년 2월 18일
댓글: Ryan 2014년 2월 18일
Probably something very basic, but I cannot see why the product is making some of the correct solutions, yet the other solution outputs 0.
Example: I placed these as my test products..
A = [2,1;3,0]
x = [2;1]
function y = myProduct(A, x)
% The command myproduct(A,x) computes the product
% of the matrix A and the vector x by column.
% Set sizes
[m, n] = size(A);
[p, q] = size(x);
% Check Dimensions
if(n == p && q == 1)
%Initializes the y vector
y = zeros(m,1);
for i = 1:m
y(n) = A(n,:)*x;
end
else
disp('Matrix Dimension Mismatch')
y = [];
end
end

답변 (1개)

the cyclist
the cyclist 2014년 2월 18일
I think you want
y(i) = A(i,:)*x;
instead of
y(n) = A(n,:)*x;
  댓글 수: 1
Ryan
Ryan 2014년 2월 18일
You are absolutely correct thanks. I actually just realized this a couple minutes ago when I wasn't even using the variable to iterate through my for loop... Thanks for the help.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by