how to find the first element that satisfy a condition in a matrix?

2 Ansichten (letzte 30 Tage)
Hallo,
I have a matrix describes the difference between secuencial elements Dwf (127*2398).
now I want to find out the first element (column-wise) that satisfy the condition < 0.01 then starting from this element I want to find the first element that is < 0 and make a new matrix limited with the elements bewteen this two conditions as in the following : ( first_element : last_element +1)
I have tried some coding but it didn't work out so far, I am still a beginner with MATLAB.
hope someone could help and thatnks in advance
for i=6:127 % the rows, I must start from the 6th row
for j = 1:2700 % the columns
first_element(:,j) = find(Dwf(:,j)> 0.01,1,'first');
last_element(:,j) = find(Dwf(:,j) < 0 & j > first_element,1,'first') ;
end
end
  4 Kommentare
Rima Habib
Rima Habib am 12 Nov. 2018
Bearbeitet: Rima Habib am 12 Nov. 2018
@Stephan, the idea was to let the programm runs for all the columns to find the requested conditions
i explained wrong above. the columns represent the time in other matrix ( that means I have 2398 values regarding specific time data stored in other matrix) but in the Dwf matrix they are actually the differences not the rows! sorry!
so i need to find the first element that satisfy the condition < 0.01 in each column then starting from this element I want to find the first element that is < 0
i used the nested loop thinking it might be easier
Rima Habib
Rima Habib am 12 Nov. 2018
Bearbeitet: Rima Habib am 12 Nov. 2018
@Matt J, you are right, i forgot about this essencial condition in matlab
in general for each column i want to get a matrix that meets the explained conditions so at the end i get n matrx each one satisfies : ( first_element : last_element +1).
so it doesnt have to be in one matrix.
i dont know if that possible?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Bruno Luong
Bruno Luong am 13 Nov. 2018
Try this
for j = 1:ny
idx_start(:,j) = find(Dwf(:,j)> 0.01,1,'first') ;
idx_end(:,j) = find (Dwf(:,j) < 0 & (1:nx)' > idx_start(:,j) , 1, 'first');
end

Weitere Antworten (1)

KSSV
KSSV am 13 Nov. 2018
Bearbeitet: KSSV am 13 Nov. 2018
YOu may follow something like below:
load('dwf.mat')
[nx,ny] = size(Dwf) ;
iwant = NaN(size(Dwf)) ;
for i = 1:ny
idx1 = find(Dwf(:,i)<0.01) ; idx1 = idx1(1) ;
idx2 = find(Dwf(:,i)<0.) ; idx2 = idx2(1) ;
iwant(idx1:idx2,i) = Dwf(idx1:idx2,i) ;
end
pcolor(iwant) ;
shading interp ;
colorbar
YOu have to check the conditions properly.
  3 Kommentare
KSSV
KSSV am 13 Nov. 2018
Bearbeitet: KSSV am 13 Nov. 2018
It works for the entire columns....see the loop.....it is across all the columns. I have edited the code for the second condition .
Rima Habib
Rima Habib am 13 Nov. 2018
Bearbeitet: Rima Habib am 13 Nov. 2018
I know i saw the loop, please bare with me, I am a beginner here
why it gives me one value idx=35 instead of a value for each column, which was i want
load('dwf.mat')
[nx,ny] = size(Dwf) ;
iwant = NaN(size(Dwf)) ;
for j = 1:ny
idx_start(:,j) = find(Dwf(:,j)> 0.01,1,'first') ;
idx_end(:,j) = find (Dwf(:,j) < 0 & j > idx_start(:,j) , 1, 'first');
end
I tried this code, for the first condition it works but for the second, i couldnt find a solution yet.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Labels and Annotations finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by