Thread Subject: Histogram

Subject: Histogram

From: Jonas

Date: 7 Nov, 2009 18:50:04

Message: 1 of 3

Does anybody have a better solution for the code below? The x- and edge-vectors has a size of 5000 elements in my problem, and the sum of k-vector is equal to the length of the x- and edge-vectors, the code allocates a lot of memory unnecessarily.

Similar problem
x = (1:1:11)';
u = [2 3.5 4 5 6 5 4 4 3 2 1]';
umin = min(u);
umax = max(u);
edge = linspace(umin,umax,numel(x))';
[k bin]=histc(u,edge);

u1 = zeros(length(k),max(k));
edge = flipud(edge);
for i=1:length(k)
    u1(i,1:k(i))=edge(i);
end
[m,n] = size(u1);
for i=1:m
    u2((i*n+1):((i+1)*n)) = u1(i,:);
end
ind1=find(u2==0);
u2(ind1)=[];
subplot(2,1,1); plot(x,u)
subplot(2,1,2); plot(x,u2)

Subject: Histogram

From: Bruno Luong

Date: 8 Nov, 2009 09:22:02

Message: 2 of 3

Try this:

x = (1:1:11)';
u = [2 3.5 4 5 6 5 4 4 3 2 1]';
umin = min(u);
umax = max(u);
edge = linspace(umin,umax,numel(x))';

%
[k bin]=histc(u,edge);
u2=sort(edge(bin),'descend')'

% Bruno

Subject: Histogram

From: Bruno Luong

Date: 8 Nov, 2009 09:35:18

Message: 3 of 3

sorry I forget to flip the edges

edge = flipud(edge);

Bruno

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com