Raja Sattiraju
Raja Sattiraju

Reputation: 1272

Sorting elements of a single array into different subarrays

I have an 1000 element array with values ranging from 1 - 120. I want to split this array into 6 different subarrays with respect to the value range

for ex:

array1 with values from ranges 0-20.

array 2 with values from range 20-40........100-120 etc.

At the end I would like to plot a histogram with X-axis as the range and each bar depicting the number of elements in that range. I dont know of any other way for 'this' kind of plotting.

Thanks

Upvotes: 0

Views: 217

Answers (2)

Luis Mendo
Luis Mendo

Reputation: 112659

If you only need the histogram, you can achieve the result using histc, like this:

edges = 0:20:120; % edges to compute histogram
n = histc(array,edges); 
n = n(1:end-1); % remove last (no needed in your case; see "histc" doc)
bar(edges(1:end-1)+diff(edges)/2, n); % do the plot. For x axis use
% mean value of each bin

Upvotes: 0

Emmet
Emmet

Reputation: 6401

In other words, you want to create a histogram. Matlab's hist() will do this for you.

Upvotes: 2

Related Questions