Arrays exercise Maximum and Minimum values

1 visualización (últimos 30 días)
Lenin Cruz
Lenin Cruz el 30 de Ag. de 2019
Editada: Stephen23 el 30 de Ag. de 2019
Hello,
I have this array
A=[1;0;0;4;5;6]
I want to get the maximum and minimum difference between the elements
MaxDiff= max(diff(A));
MinDiff= min(diff(A));
I would like to omit 'zero' in the calculations, so my output look like the two lines below. Which is not possible with the piece of code above
MaxDiff= 5
MinDiff= 1
Is there any way to do that without changing the size of my matrix A? it should be always 6 for future applications.
Thanks

Respuestas (1)

Stephen23
Stephen23 el 30 de Ag. de 2019
Editada: Stephen23 el 30 de Ag. de 2019
>> B = nonzeros(A(:));
>> M = bsxfun(@minus,B,B.');
>> V = abs(M(~eye(numel(B))));
>> max(V)
ans = 5
>> min(V)
ans = 1

Categorías

Más información sobre Operators and Elementary Operations en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by