How to separate a column into two table of multiple columns?

1 view (last 30 days)
Hello! And thank you for helping! I have a double column that looks like this:
A
58
18
18
48
18
NaN
NaN
NaN
18
15
NaN
NaN
Of more than 16000 rows. I would like to be able to separate the indexes in two tables, one table for the indexes of values and one table for the indexes of NaN. Moreover, I would like it to change column each time the value passes to a NaN and vice versa, as seen below:
Table of values:
1 9
2 10
3
4
5
and the table of Nan would be:
6 11
7 12
8
I tried this but it doesnt work:
index1=5990; % start row for the sum of hours
index2=6068; % end row
i2=1;
i3=1;
i4=1;
i5=1;
for i1=index1:index2
if ~isnan(ActivePuissance(i1,1))
Fidx(i2,i3)=i1;
i2=i2+1;
else
NFidx(i4,i5)=i1;
i4=i4+1;
end
% i3=i3+1;
%i5=i5+1;
end

Accepted Answer

Guillaume
Guillaume on 6 Apr 2017
Edited: Guillaume on 6 Apr 2017
Note that table is a particular type of container in matlab. It does not look like it's what you mean by table in your question.
I'm not sure what it is you want as an output. You can't have a matrix with different number of rows per column. Assuming you want a single cell array as output:
function C = splitatdiscontinuites(V)
%V a column vector of strictly increasing integers
%C a cell array of the same integers split into columns of constant increment of 1
validateattributes(V, {'numeric'}, {'integer', 'increasing', 'column'})
runlengths = diff([0; find(diff(V) > 1); numel(V)]); %find length of each continuous run of number
C = mat2cell(V, runlengths, 1)'; %split into cell arrays
%concatenate into a 2D cell array with empty cells:
maxheight = max(cellfun(@numel, C));
C = cellfun(@(c) [num2cell(c); cell(maxheight-numel(c), 1)], C, 'UniformOutput', false);
C = [C{:}];
end
You can use this function to find both of your outputs:
A = [58 18 18 48 18 NaN NaN NaN 18 15 NaN NaN]'
splitatdiscontinuites(find(~isnan(A)))
splitatdiscontinuites(find(isnan(A)))
Note however that the whole thing does not sound like a good idea. What is the use case?

More Answers (0)

Community Treasure Hunt

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

Start Hunting!