Spyros
Spyros

Reputation: 289

Plot bars in MATLAB

I have a matrix(4,100) in MATLAB. Each one of its column are in such way that the 1st element matrix(1,i) is the smaller and the 4th element matrix(4,i) is the bigger. Something like

    matrix(:,1) = - 0.3; 0,4; 0,4; 0,9

How can I do a bar graph were I can plot as a bar the distance between the two edges?

each column has to be represented by on bar in order to make 100 bars at the end.

Thanks

Upvotes: 1

Views: 291

Answers (2)

Luis Mendo
Luis Mendo

Reputation: 112659

I'm not sure if this is what you want, but you can plot all bars in different colors, from the largest (last row of matrix) to the smallest (first row of matrix), so that the smaller ones get stacked over but let the larger ones to be seen:

matrix = [.1 .2 .3 .4 .5
          .2 .3 .5 .6 .7
          .4 .4 .8 .7 .8
          .5 .6 .9 .8 .9]; %// example data
hold on
colors = {'r','g','b','c'}; %// define colors
for n = size(matrix,1):-1:1 %// iterate over rows, from last to first
    bar(matrix(n,:), colors{n})
end

enter image description here

Upvotes: 2

Ray
Ray

Reputation: 2510

I assume by edges you mean the first and fourth element.

You can use:

diff = abs(matrix(1,:) - matrix(4,:)); // distance
bar(diff); // bar plot

Upvotes: 0

Related Questions