필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
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

채택된 답변

Jan
Jan 2020년 11월 27일
Get the corresponding indices:
first = find(X, 1, 'first');
last = find(X, 1, 'last');
Result = X(last) - X(first)

추가 답변 (1개)

Ameer Hamza
Ameer Hamza 2020년 11월 27일
편집: Ameer Hamza 2020년 11월 27일
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

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by