简体   繁体   English

Matlab中二维矩阵元素的直方图

[英]Histogram on elements of a 2D matrix in Matlab

I am wondering if there is any build in function or an easy way to plot a histogram of elements of a 2d array . 我想知道是否有任何功能构建或一个简单的方法来绘制2d array元素的直方图。

For example, if A=rand(100,1) , then A is an 1D array , and hist(A) can do the histogram. 例如,如果A=rand(100,1) ,则A1D arrayhist(A)可以进行直方图。

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 . 但是,如果A=rand(100,100) ,我想对A元素进行直方图,就像将A中的每个元素视为一1D array的元素一样。 Is there a easy way to do so? 有一个简单的方法吗?

你只需要将A重塑为一个向量,然后就像往常一样使用hist

hist(A(:))

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: 它的作用是通过将矩阵A重新塑造成一个矩阵来创建一个向量,该矩阵有一列,行数等于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. 这会依次获取A的每个元素,从而也生成一个向量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM