How to select specific cells rows in a column

조회 수: 17 (최근 30일)
Louis Cook
Louis Cook 2019년 1월 4일
댓글: Cris LaPierre 2019년 1월 7일
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

답변 (2개)

per isakson
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
  댓글 수: 1
Louis Cook
Louis Cook 2019년 1월 6일
편집: Louis Cook 2019년 1월 6일
Sorry didnt know to re ask in here :)
So i tried the above option into my code but it wasnt really working so I guess its easier to put my entire code into this and hopefully there is an easier solution :)
1.csv is an example of a file in which there is only one group of 0's so there is no problem
10.csv is an example of where this is two groups of 0's which is causing a problem
thanks for the help
g=9.81; %acceleration due to gravity
num = 1:78;
ext = ".csv";
JumpData = num + ext;
for f=1:length(JumpData );
inputdata=xlsread(JumpData{f});%extract all data from first jump%
idx = find(inputdata(6:3000,5)==0);%finds all the zeros in the z collumn%
zeroMatrix = inputdata(idx,5);%Turns it into a matrix%
oneMatrix=zeroMatrix+1;%adds to to all the zeros%
t=sum(oneMatrix)/100;%sum all of the zeros and turn into miliseconds%
MH(f)=0.5*g*(0.5*(t^2));
MHFinal=MH';
end

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


Cris LaPierre
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
Cris LaPierre 2019년 1월 7일
EDIT: Changing your answer to a comment
Answer by Louis Cook on 6 Jan 2019 at 11:55
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
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.
You can learn more about indexing here.

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by