Info

This question is closed. Reopen it to edit or answer.

Need help with matlab

1 view (last 30 days)
last middle
last middle on 30 Nov 2014
Closed: MATLAB Answer Bot on 20 Aug 2021
Its not working for some reason. Can any one help.
A= [21 22 21 26 31 35 59 89 45 35 20 13
21 22 18 24 32 36 58 88 46 34 21 23
15 14 17 28 33 37 57 87 47 33 22 43
17 15 24 21 34 38 56 86 48 32 23 3
19 18 19 23 35 39 55 85 49 31 21 9
14 14 21 29 36 30 54 84 50 30 24 8
22 21 14 21 37 31 53 83 51 31 25 7
21 18 16 25 38 32 52 82 52 32 19 6
23 15 17 24 39 33 51 81 53 33 78 5
21 19 15 27 38 34 51 80 54 34 17 4
20 14 18 20 37 35 50 79 55 35 16 13
];
GRAPH = A(1:11,1:12);
if GRAPH < 15
bar(GRAPH,'b')
hold on
else if GRAPH >= 16 & GRAPH <= 31
bar(b,'g')
else if GRAPH >= 32 & GRAPH <= 49
bar(c,'y')
else if GRAPH >=50 & GRAPH<=99
bar( GRAPH,'k')
else if GRAPH >=100
bar( GRAPH,'r')
end
end
end
end
end
hold off
  1 Comment
Stephen23
Stephen23 on 1 Dec 2014
"Its not working" does not tell us what this code should do... and we cannot read your mind. What output do you expect to get?

Answers (2)

Guillaume
Guillaume on 30 Nov 2014
Edited: Guillaume on 30 Nov 2014
It's not working is not very helpful. We can't read your mind and know what it should be doing.
With that proviso, I suspect that your problem is with your if statements,
if matrix < scalar
is equivalent to
if all(matrix(:) < scalar)
That is all the elements of matrix must be smaller than scalar.
As I've no idea what you're trying to do with the comparisons, I can't tell you how to fix it.
-----
Side note:
if ...
else if ...
else if ...
end
end
end
is better written as:
if ...
elseif ... %note it's one word
elseif ...
end %only one end

Star Strider
Star Strider on 30 Nov 2014
I’m guessing here as to what you want to do. See if this is it:
GRAPH = A;
bar(GRAPH(GRAPH<15),'b')
hold on
bar(GRAPH(GRAPH >= 16 & GRAPH <= 31),'g')
bar(GRAPH(GRAPH >= 32 & GRAPH <= 49),'y')
bar(GRAPH(GRAPH >=50 & GRAPH<=99),'k')
bar(GRAPH(GRAPH >=100),'r')
hold off

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!