convert cell into double with diferent cell sizes

3 次查看(过去 30 天)
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 个评论
Bob Thompson
Bob Thompson 2018-3-2
编辑:Bob Thompson 2018-3-2
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
Tiago Dias 2018-3-5
编辑:Tiago Dias 2018-3-5
I want to store the index on NaN values, the way I do I store that in a cell, but I want to store if possible in a double, table or matrix. just that. But my input have values and NaN values numbers.
The code you wrote gets me an error:
Subscripted assigment dimension mismatch
Error in (line 5)
locationarray(:,I) = res{I};
If i use
locationarray = res{I}
It only store in location array the information when I=2, doesnt save for I=1

请先登录,再进行评论。

采纳的回答

Jos (10584)
Jos (10584) 2018-3-5
You can my PADCAT function to concatenate vectors with different lengths. It is available at the Matlab File Exchange:
However, there are simpler ways to get the output res, from A:
A = [1,2;2,4;3,NaN;NaN,5;NaN,7]
res = repmat(1:size(A,1),size(A,2),1)' ;
res(isnan(A)) = NaN
res = sort(res)

更多回答(1 个)

Tiago Dias
Tiago Dias 2018-3-5
Thanks, works perfect! Will try on my big data to see if is good

类别

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