I have an array which is increasing. At one point it starts to decrease. This trend starts repeating many times in array. How to extract only the decreasing portion from that array?

I have an array which is increasing. At one point it starts to decrease. This trend starts repeating many times in array. How to extract only the decreasing portions from that array and plot another array values which corresponds to that decreasing portion?

 채택된 답변

d = diff( YourArray(:).' ); %I assume it is a vector of unknown orientation
goes_down = strfind([0 d>0], [1 0]) + 1;
stops_going_down = strfind([0 d<0], [1 0]);
n_seg = length(goes_down);
segs = cell(1, n_seg);
for K = 1 : n_seg;
segs{K} = YourArray(goes_down(K) : stops_going_down(K));
end

댓글 수: 7

TESNA, please attach your data, or at least a screenshot of it. If it's noisy, you may have to apply denoising methods first to get results that are what you're hoping for.
Third column is the increasing and decreasing array from which decreasing values are to be extracted and I have to plot corresponding values of the second column..
It is not clear how you want the values to be plotted against each other.
fid = fopen('2variables.txt', 'rt');
YourTable = textscan(fid, '%s%f%f', 'Headerlines', 1);
fclose(fid);
Col2 = YourTable{2};
Col3 = YourTable{3};
d = diff( Col3 .' ); %I assume it is a vector of unknown orientation
mask = d < 0;
goes_down = strfind([0 mask], [0 1]) + 1;
stops_going_down = strfind([mask 0], [1 0]) + 1;
n_seg = length(goes_down);
idx2s = cell(1, n_seg);
seg2s = cell(1, n_seg);
seg3s = cell(1, n_seg);
for K = 1 : n_seg
idx2s{K} = goes_down(K) : stops_going_down(K);
seg2s{K} = Col3(idx2s{K});
seg3s{K} = Col2(idx2s{K});
end
segs23 = [seg2s; seg3s];
plot(segs23{:});
Thank you for your help. It works.
Please mark the Answer as Accepted if this is what you were looking for.
... I still wonder whether perhaps you want to plot
segs32 = [seg3s; seg2s];
plot(segs32{:});
Hard to say... neither one is all that clear.
How can I apply this code in multiple files?
d = diff( Col3 .' ); %I assume it is a vector of unknown orientation
mask = d < 0;
goes_down = strfind([0 mask], [0 1]) + 1;
stops_going_down = strfind([mask 0], [1 0]) + 1;
n_seg = length(goes_down);
idx2s = cell(1, n_seg);
seg2s = cell(1, n_seg);
seg3s = cell(1, n_seg);
for K = 1 : n_seg
idx2s{K} = goes_down(K) : stops_going_down(K);
seg2s{K} = Col3(idx2s{K});
seg3s{K} = Col2(idx2s{K});
end
segs23 = [seg2s; seg3s];
When you process the multiple files, at each step do you want to forget about the previous file (except perhaps for the plot that has been drawn), or at each step do you want to add on to the seg2s / seg3s arrays so that you end up with one long cell array of those that includes all of the files, or do you want to use a cell array wrapping around each of those so that you can index into the cell array to get the seg2s / seg3s relevant to that file, or ....?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

질문:

Tez
2017년 7월 9일

댓글:

2017년 7월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by