splitting data in a table when ever the cell is empty

조회 수: 13 (최근 30일)
Sufia Fatima
Sufia Fatima 2019년 3월 18일
댓글: Walter Roberson 2019년 3월 19일
Hi
I have a long set of data where 100 cycles appear in one colum under each other. Each cycle varies in length.
However they are split up with two empty cells etween each data set.
I want to split the data so the 100 cycles appear next to each other so instead of one colum with all the data there is 100. ( not necerssary but if possible ideally i would want to create a 'cell' numbered 1-100 in first colum with the data in the second colum.)
The following is just an example of what i want.3 cycles side by side.
x
10 20
20 6
30 8
40 7
50 5
40 4
20 6
45 0
4 3
30 6
8 8
10 20 50 5 45 0
20 6 40 4 4 3
30 8 20 6 30 6
40 7 8 8
  댓글 수: 6
Guillaume
Guillaume 2019년 3월 18일
So your data is stored in a matrix. Matrices don't have empty cells. I'm confused by your question.
Sufia Fatima
Sufia Fatima 2019년 3월 18일
There are defo empty rows. for example 15811 and 17030.
wherever there is an empty row i want to split the data to signify a diff test.

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

채택된 답변

Guillaume
Guillaume 2019년 3월 18일
Matrices can never have empty rows. What you call empty rows are rows with 0s in both columns.
If I understood correctly what you want to do:
%indentation_3hr: input matrix
zerorows = find(all(indentation_3hr == 0, 2)); %find rows with 0 in both columns
rowdist = diff([0; zerorows; size(indentation_3hr, 1)]); %distance between the rows containing 0
splitted = mat2cell(indentation_3hr, rowdist, size(indentation_3hr, 2)); %split into cell array at the 0s
splitted = cellfun(@(m) m(1:end-1, :), splitted, 'UniformOutput', false); %remove last row of each matrix (as it's [0 0])
splitted(cellfun(@isempty, splitted)) = []; %remove empty matrices (due to the double rows of [0 0])
maxheight = max(cellfun(@(m) size(m, 1), splitted)); %get height of the tallest matrix
padded = cellfun(@(m) [m; nan(maxheight - size(m, 1), size(m, 2))], splitted, 'UniformOutput', false); %pad shorter matrices with nan
newmatrix = [padded{:}]; %concatenate all padded matrices horizontally
newmatrix has 200 columns (2 columns per cycle). Shorter cycles are padded with NaN (since again, matrices cannot have empty rows).

추가 답변 (1개)

Walter Roberson
Walter Roberson 2019년 3월 18일
편집: Walter Roberson 2019년 3월 18일
mask = ~any(indentation_3hr, 2).'; %want row vector
starts = strfind([false mask],[0 1]); %start of non-empty groups
stops = strfind([mask false], [1 0]); %end of non-empty groups
extracted = arrayfun(@(FROM, TO) indentation_3hr(FROM:TO, :), starts, stops, 'uniform', 0);
maxlen = max(arrayfun(@(V) size(V,1), extracted));
FirstN = @(M,N) = M(1:N,:);
PadN = @(M,N) FirstN([M;nan(N, size(M,2))], N);
result = cell2mat( cellfun(@(M) PadN(M, maxlen), extracted(:).', 'uniform', 0) );
  댓글 수: 2
Sufia Fatima
Sufia Fatima 2019년 3월 19일
Hi Thank you for your help. However, I have this error when i use this code
Error: File: indentation.m Line: 15 Column: 17
Incorrect use of '=' operator. To assign a value to a variable, use '='. To compare values for equality, use '=='.
This is in relation to
FirstN = @(M,N) = M(1:N,:);
I have done what was suggested with inputting
FirstN = @(M,N) == M(1:N,:);
However, i still get the error
Error: File: indentation.m Line: 15 Column: 17
Invalid use of operator.
Again Thanks for the suggestion
Walter Roberson
Walter Roberson 2019년 3월 19일
FirstN = @(M,N) M(1:N,:);

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by