## Having trouble with a short code

Asked by D. H.

### D. H. (view profile)

on 9 Nov 2018 at 20:17
Latest activity Closed by D. H.

### D. H. (view profile)

on 11 Nov 2018 at 19:45
Accepted Answer by Stephen Cobeldick

### Stephen Cobeldick (view profile)

Hi, I have a short code...

dpb

### dpb (view profile)

on 10 Nov 2018 at 16:18
And what seems to be the Q?
I'll note there doesn't seem that there should be any reason to have to run this in a loop; that's very inefficient and doesn't make any use at all of the primary feature of Matlab; MATrix LABoratory. Octave is a work-alike with similar features; learn to use it as intended as well.
HINT:
While I didn't study the code at all thoroughly, the if block in the plotting is very poorly structured as multiple separate blocks for all cases instead of as one construct. They could possibly be part of the problem altho as noted I didn't read it carefully enough to be sure one way or the other; just that it's awkward at best.
Logical addressing would be "the Matlab way" for such an operation.

### Stephen Cobeldick (view profile)

Answer by Stephen Cobeldick

### Stephen Cobeldick (view profile)

on 10 Nov 2018 at 17:26
Edited by Stephen Cobeldick

### Stephen Cobeldick (view profile)

on 10 Nov 2018 at 22:15

% 1)
M0 = [1,4,3,100,12,1000,25,32,500,700,20,9000,25,650,40,3,67,2,305,80,200,50,2,15,4];
M1 = [1,6,2,120,14,900,25,35,550,650,23,7500,25,700,43,6,72,8,360,70,220,60,1,14,8];
M2 = [1.5,5,7,150,13,550,1,40,520,600,27,5500,27,900,44,12,75,9,360,50,190,50,4,13,6];
M3 = [122,120,120,170,140,700,100.5,134,570,600,125,6100,125,1050,122,130,187,109,460,145,330,160,105,113,104];
M4 = [25,25,30,40,50,600,6,33,500,600,26,5500,26,1150,14,32,126,8,360,28,230,60,2,13,5];
% 2)
M = [M0;M1;M2;M3;M4];
% 3)
M = M - (M(:,end)-mean(M(:,end)));
% 4a)
RM = (M - min(M,[],1)) ./ mean(M,1);
% 4b)
ARM = RM + (1 - RM(1,:));
% 5)
idr = ARM(5,:)>1.3;
idb = ARM(5,:)<0.7;
idg = ~idr & ~idb;
plot(ARM(:,idr),'r',ARM(:,idb),'b',ARM(:,idg),'g')
Note:
1. The deadline for this homework was yesterday.
2. Your professor/tutor can find this website just as easily as you can.
3. Submitting work that is not your own without acknowledgement is plagiarism.