In a column how do I find the first cell to equal 0?

2 Ansichten (letzte 30 Tage)
Franchesca
Franchesca am 24 Apr. 2014
Beantwortet: per isakson am 24 Apr. 2014
I have this code so far:
for i = 7:length(mydata)
index = find (mydata{i,1}(:,5))== 0
end
However, this returns all the zeros in the whole matrix, I require a cell reference in column 5 as to when the first 0 occurs.
  4 Kommentare
Franchesca
Franchesca am 24 Apr. 2014
%% Calculate peak power
[row col] = ((mydata{i,1}(:,5))== 0,'first'); % finding the cell which is first 0 in column
pp(i,1) = nanmin (mydata {i,1}(:,5)); % finding peak power
pp is finding the peak power in the whole of column 5 however I want to find the first zero then find peak power up to this point
Christopher Pedersen
Christopher Pedersen am 24 Apr. 2014
Bearbeitet: Christopher Pedersen am 24 Apr. 2014
Finding the first zero: you can use additional inputs to "find", to only return the first value found:
loc = find(x==0,1,'first') returns the 1 first times x==0
or loc = find(x==0); loc = loc(1); returns all the occurences of zero, then extracts the first.
Extracting values up to the first zero: y = x(1:loc);

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

per isakson
per isakson am 24 Apr. 2014
Run this
ixc = 3; % third column
sample_array = num2cell( magic(4)-3 );
is = cell2mat( sample_array( :, ixc ) ) == 0;
>> sample_array
sample_array =
[13] [-1] [ 0] [10]
[ 2] [ 8] [ 7] [ 5]
[ 6] [ 4] [ 3] [ 9]
[ 1] [11] [12] [-2]
>> is
is =
1
0
0
0

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by