Home script to calculate the dot product of a column vector
Reply: 0

script to calculate the dot product of a column vector

user1039
1#
user1039 Published in April 21, 2018, 4:00 am

This is my code. It keeps giving an error that Index exceeds matrix dimensions.

function [y_out] = my_dot(x,y)
    %for finding the inner product or dot product of two arrays
    %first need to know the size

    [m_x,n_x] = size(x);
    [m_y,n_y] = size(y);
    %ensure is row or column vectors of equal length
    if(m_x ~= 1 && n_x ~=1) || (n_y ~= 1 && n_y ~= 1)
        y_out = 'Failed';
        return
    end

    %determine if x and y are column or row vectors
    %both should be column vectors
    if (m_x == 1 || m_y ==1) %they are row vectors
        y_out = 'Failed';
        return
    end

    %make sure x and y are the same size
    if n_x ~= n_y
         y_out = 'Failed';
         return
    end

    %now do the dot product
    for i = 1:m_x
        y = sum(x(i,1) * y(i,1));
        y_out = y;
    end
end

this is what I get

    >> my_dot([1;1],[2;3])
    Index exceeds matrix dimensions.

    Error in my_dot (line 26)
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.309556 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO