Elitism in Matlab?

How to implement elitism in Matlab? For example, I run the program and after each run the value is stored in the say a variable and after all the runs, say, 6 starts, I have the afollowing

a = [7, 5, 4, 3, 6, 8];

how can I apply elitism ato the end of the content aasa = [7, 5, 4, 3, 3, 3];

That is, when I look through a, I replace the larger number with the smaller one that I come across. From example, after scanning through a, 5<7so I remain 5, 4<5so I hold 4, 3<4so I hold 3, 3<6so I replace 6on 3again 3< 8, so I substitute 8on 3until the end of abotha = [7, 5, 4, 3, 3, 3];

how to do it in matlab.

Attempt

I said:

if a(i)< a(i+1)
a(i+1) = a(i);
end

plot(a); 

so that I can have a schedule that runs smoothly.

but I keep having the following error:

'Subscript indices must either be real positive integers or logicals.'

Any idea how I can do this right.

+4
source share
3 answers

I believe this should work in all cases:

b = [a(1), arrayfun(@(n) min(a(1:n)), 2:length(a))]

a =
     7     4     3     6     5     2     5
b =
     7     4     3     3     3     2     2

For reference:

Your initial thought was correct, but you forgot to put it ifinside the loop. You could do:

for ii = 1:length(a)-1
   if a(ii)< a(ii+1)
      a(ii+1) = a(ii);
   end
end

The reason you got the error was because you probably didn’t identify it i, so MATLAB interpreted it as an imaginary unit ( sqrt(-1)). This is also the reason I used iiinstead iin a loop to avoid such errors.

+4
source

:

a = [7, 5, 4, 3, 6, 8];
[y, i] = findpeaks(-a, 'npeaks', 1) ;
a(i:end)=-y;
plot(a)

:

a plotted

+2

Alternative Robert P. answer without arrayfun:

min(triu(repmat(a(:),1,numel(a))) + tril(NaN(numel(a)),-1))
+2
source

All Articles