Histogram on elements of a 2D matrix in Matlab

自古美人都是妖i 提交于 2019-12-07 18:40:09

问题


I am wondering if there is any build in function or an easy way to plot a histogram of elements of a 2d array.

For example, if A=rand(100,1), then A is an 1D array, and hist(A) can do the histogram.

However, what if A=rand(100,100), and I would like to make a histogram on elements of A, just like treating each element in A as an element on a 1D array. Is there a easy way to do so?


回答1:


You just have to reshape A into a vector, then you can use hist as usual:

hist(A(:))



回答2:


This command will do what you want:

hist(reshape(A, prod(size(A)), 1))

What it does is create a vector out of the matrix A by reshaping it into a matrix with one column and a number of rows equal to the number of elements of A:

prod(size(A)) = number_of_columns(A) * number_of_rows(A)

Or the short way:

hist(A(:))

This takes every element of A in sequence and thus also generates a vector.



来源:https://stackoverflow.com/questions/5568516/histogram-on-elements-of-a-2d-matrix-in-matlab

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!