convert cell into double with diferent cell sizes
Show older comments
Hello, for example i have
A = [1,2;2,4;3,NaN;NaN,5;NaN,7]
res = cellfun(@(x) find (~isnan(x)),num2cell(A,1),'un',0) % Here i store in the index of the numbers that are not NaN
res is a 1x2 cell array, the first component is a 3x1 double and the second one is a 4x1 double.
res{1} = 1
2
3
res{2} = 1
2
4
5
My question is to do a table or a double, or a matrix that each res{j} is stored in the respective j column of the new table I tried a cell2mat, but since they aren't the same size it doesn't work. the goal is to make
output = 1 1
2 2
3 4
(empty or NaN or whatever) 5
5 Comments
Bob Thompson
on 2 Mar 2018
Edited: Bob Thompson
on 2 Mar 2018
I know that it's possible to pad an array, but I'm not sure of the code. I would suggest making an array with the number of columns equal to the largest cell in res, then filling in the values from the cells of res. All extra elements will be NaN.
array = NaN(max(size(res{:})),size(res));
for I = 1:size(res);
array(:,I) = res(I);
end
I also can't guarantee that size works properly with cell arrays, but that should at least get you started.
Tiago Dias
on 2 Mar 2018
Edited: Stephen23
on 2 Mar 2018
Tiago Dias
on 2 Mar 2018
Edited: Tiago Dias
on 2 Mar 2018
Bob Thompson
on 2 Mar 2018
Edited: Bob Thompson
on 2 Mar 2018
Are you actually trying to get rid of all of the NaN values, or just gather the non-NaN values together? If you're just trying to gather the values try using the following:
A = [1,2;2,4;3,NaN;NaN,5;NaN,7]
res = cellfun(@(x) find (~isnan(x)),num2cell(A,1),'un',0)
locationarray = NaN(size(A)); % Create an array the size of A, but filled with NaN values
for I = 1:size(A,2) % Run index I through each column of A, this should correspond to each cell in res
locationarray(:,I) = res{I}; % Put cell I of res into column I of the location array. Because it's in a for loop it will cover all columns
end
Tiago Dias
on 5 Mar 2018
Edited: Tiago Dias
on 5 Mar 2018
Accepted Answer
More Answers (1)
Tiago Dias
on 5 Mar 2018
0 votes
Categories
Find more on Data Type Conversion 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!