Search code examples
matlabscatter-plot

Matlab scatter two matrices - issue


I am trying to figure out how the scatter function works in MATLAB. For example , I have the two matrices:

 mat1 = rand(20,20);
 mat2 = rand(20,20);

At this point I need to open a figure and use the scatter function to display a scatter plot of the values in mat1 versus the values in mat2. What I did is:

figure()
scatter(mat1,mat2)

Obvious that is wrong. But I don't know how to do that. In addition I read the documentation about the scatter function in the MATLAB docs Scatter Function - MATLAB DOCS


Solution

  • Since scatter function expects vectors (Matrixes with either one row or one column), try

    figure()
    scatter(mat1(:),mat2(:))
    

    The (:) operator turns matrixes into vectors.