How to generate a loop using logical indexes intervals?

조회 수: 9 (최근 30일)
Gustavo Oliveira
Gustavo Oliveira 2015년 6월 22일
댓글: Star Strider 2015년 6월 23일
Hi all,
I generated an index based on the depth position in which one sensor changes the direction at the water column, from upward to downward (Code below). My idea is to apply this index to locate the position of the MAXIMUM density between each index. For example, I would like to know the position of MAXIMUM density from index 1:ind(1), ind(1):ind(2), ind(2):ind(3)... The index interval are not regular and I am processing a 400810x1 size data. The figure below illustrate what I am planning to obtain, the values in red are the maximum differences in each group and I am interested to obtain the "Position" of the same. The position will work as index to continue the data processing.
Thank you for your time,
%Locating the position of change in direction based on depth differences
ind_change_direc=nan(size(depth_diff));
for i=1:length(depth_diff)-1
if depth_diff(i)<0 & depth_diff(i+1)>0;
ind_change_direc(i)=-9999;
end
end
index=ind_change_direc==-9999;

채택된 답변

Star Strider
Star Strider 2015년 6월 23일
Another possibility:
index = [-9999; NaN(7,1); -9999; NaN; NaN; -9999];
density = rand(12,1)*0.01; % Create Density
position = [1:12]';
change_idx = find(~isnan(index));
for k1 = 1:length(change_idx)-1
[mx,idx] = max(density(change_idx(k1):change_idx(k1+1)-1));
max_density(k1,:) = [mx, position(idx+change_idx(k1)-1)];
end
The ‘max_density’ matrix gives the maximum in the interval in the first column, and the value of ‘position’ for it in the second.
  댓글 수: 3
Gustavo Oliveira
Gustavo Oliveira 2015년 6월 23일
The Code Below worked well to obtain the position that I was expecting ("Final Result").
Thank you again,
P=1:length(diff_dens);
P=P';
A=max_density(:,2);
for i=A(1:end)
P(i)=-9999;
end
Star Strider
Star Strider 2015년 6월 23일
My pleasure.

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

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2015년 6월 22일
s=[1 0 2 3 4 3 2 1 0 5 7 8 9 4 3 0 1] % Example
idx1=sign([diff(s) ])
idx1=idx1([1 1:end])
position=find(diff(idx1)~=0)+1
  댓글 수: 1
Gustavo Oliveira
Gustavo Oliveira 2015년 6월 23일
Thank you for your answer Azzi. However the main intention of the routine is to identify the maximum value in each group market by the indexes (index=ind_change_direc==-9999). I did not see how the command that you wrote can work on this. I am thinking in execute a for loop using the index interval and selecting the maximum value of density, but I do not really know how.
Regards

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by