how do i substract the first value from the last one on an array taking out zero values?

1 visualización (últimos 30 días)
Good morning people,
How could I substract the first value minus the last one of just the non zero values of an array?
How could I substract 20.08794 (first non zero value) to the last non zero value (23.57)?
Thanks in advance,
Have a good one
0
0
0
20.0879453799688
20.2939815063105
20.5010823257263
20.7087372901217
20.9165579913015
21.1228352488585
21.3295563001918
21.5354478396482
21.7403423949296
21.9443776718216
22.1482301195365
22.3520614637941
22.5558741325109
22.7596657087283
22.9635408028690
23.1674804705021
23.3714846914593
23.5754962282169
0
0
0
0

Respuesta aceptada

Jan
Jan el 27 de Nov. de 2020
Get the corresponding indices:
first = find(X, 1, 'first');
last = find(X, 1, 'last');
Result = X(last) - X(first)

Más respuestas (1)

Ameer Hamza
Ameer Hamza el 27 de Nov. de 2020
Editada: Ameer Hamza el 27 de Nov. de 2020
If all the non-zero values are in increasing order, the try this
M(M~=0) = M(M~=0) - min(M(M~=0));
Result
>> M = [
0
0
0
20.0879453799688
20.2939815063105
20.5010823257263
20.7087372901217
20.9165579913015
21.1228352488585
21.3295563001918
21.5354478396482
21.7403423949296
21.9443776718216
22.1482301195365
22.3520614637941
22.5558741325109
22.7596657087283
22.9635408028690
23.1674804705021
23.3714846914593
23.5754962282169
0
0
0
0];
>> M(M~=0) = M(M~=0) - min(M(M~=0));
>> M
M =
0
0
0
0
0.2060
0.4131
0.6208
0.8286
1.0349
1.2416
1.4475
1.6524
1.8564
2.0603
2.2641
2.4679
2.6717
2.8756
3.0795
3.2835
3.4876
0
0
0
0

Categorías

Más información sobre Matrices and Arrays 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