How to select specific cells rows in a column
조회 수: 17 (최근 30일)
이전 댓글 표시
So i currently have an array that is similar to this on a much larger scale:
1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
I need to write a code that picks out all of the zeros in colum 5 ( the ones that are bolded) but I need it to ignore any subseuent zeros after that (the two rows after)
any help would be great
댓글 수: 0
답변 (2개)
per isakson
2019년 1월 5일
편집: per isakson
2019년 1월 6일
%%
M = [
1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
];
%%
isZero = M(:,5)==0;
% position of first zero
ixb = find( isZero, 1, 'first' );
% position of last zero in first sequence of zeros
ixe = find( not(isZero(ixb:end)), 1, 'first' ) + ixb - 2;
%%
z5 = reshape( M(ixb:ixe,5), 1,[] )
returns
z5 =
0 0 0
Cris LaPierre
2019년 1월 6일
편집: Cris LaPierre
2019년 1월 6일
I'm a fan of readtable, so although it takes more setup, I think overall it handles tables better.
Here is code that just finds the first set of zeros. I'll let you add the rest of your code to it.
for f=1:length(JumpData)
% Set import options to capture information in headerlines
opts = detectImportOptions(JumpData{f});
opts.VariableDescriptionsLine = 3;
opts.VariableNamesLine = 4;
opts.VariableUnitsLine = 5;
opts.DataLines = 6;
% load data into a table
data = readtable(JumpData{f},opts,'ReadVariableNames',true);
% find first grouping of zeros in Fz
idx = find(data.Fz == 0);
step = find(diff(idx)>1,1)+1;
idx(step:end)=[];
% convert length of zeros to time aloft (each 0 corresponds to 0.01 seconds?)
tm(f) = length(idx)*0.01;
end
댓글 수: 2
Cris LaPierre
2019년 1월 7일
EDIT: Changing your answer to a comment
Hello thankyou for your response,
the code works near enough perfect :) the only problem is that the output (tm(f)) is coming out a 1x2 matrix and i dont see why as it is only asking for the length of idx.
Cris LaPierre
2019년 1월 7일
How many files were in JumpData? I'm guessing 2? The code I wrote will make tm a 1xf vector, where f is the number of files in JumpData.
I naively assumed you would want to capture the time for each file. The variable f is your loop counter, so tm(1) is the time in JumpData{1}, tm(2) is the file in JumpData{2}, etc.
If that is not the case, you will need to modify the code I shared to fit your use case.
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!