MATLab matrix multiplier not outputting the correct product..

1 view (last 30 days)
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

Answers (1)

the cyclist
the cyclist on 18 Feb 2014
I think you want
y(i) = A(i,:)*x;
instead of
y(n) = A(n,:)*x;
  1 Comment
Ryan
Ryan on 18 Feb 2014
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.

Sign in to comment.

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!