How to select specific cells rows in a column
16 views (last 30 days)
Show older comments
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 Comments
Answers (2)
per isakson
on 5 Jan 2019
Edited: per isakson
on 6 Jan 2019
%%
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
1 Comment
Cris LaPierre
on 6 Jan 2019
Edited: Cris LaPierre
on 6 Jan 2019
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 Comments
Cris LaPierre
on 7 Jan 2019
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
on 7 Jan 2019
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.
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!