Find NaN values in cell array that stores changing types of elements over time

1 view (last 30 days)
I have a 1x2 cell array (paths_upd) that stores the output of dijkstra's algorithm for two different cases. A single output(paths_upd{n} is usually also a cell array with vectors stored inside.
These vectors represent paths in a network between nodes (source nodes and target nodes; similar to an adjaceny matrix). Whenever there is no connection between 2 nodes NaN will be returned instead.
An example for paths_upd{n}:
>> paths_upd{1}
ans =
2×4 cell array
{1×2 double} {1×3 double} {1×3 double} {1×4 double}
{1×3 double} {1×2 double} {1×3 double} {1×2 double}
I want to identify any target nodes that are entirely disconnected. If in the column of a target node only NaN entries exist, there is no connection to that target node from any of the source nodes.
I have successfully used the following code to identify these target nodes:
f=target_nodes(sum(cell2mat(cellfun(@(x) any(~isnan(x)),paths_upd{1},'UniformOutput',false)),1)==0);
For the case that only one source node and one target node exist and they are disconnected the function will return NaN (double) instead of a cell array. In this case I get the following error message:
Input #2 expected to be a cell array, was double instead.
How do I best proceed to identify the disconnected target nodes for both case?
  2 Comments
dpb
dpb on 21 Jul 2018
Show the code that generates the result to be tested; probably the way to solve the problem is to ensure it always returns the same type; perhaps you'll have to special-case it there.
I'd choose to do it at that point rather than make the fixup here, later...

Sign in to comment.

Accepted Answer

dpb
dpb on 21 Jul 2018
I'd still fix it there...the very last thing in the function
function [costs,paths] = dijkstra(AorV,xyCorE,SID,FID,showWaitbar)
...
is--
% Pass the path as an array if only one source/destination were given
if L == 1 && M == 1
paths = paths{1};
end
I'd simply comment that out and all should be well and the outputs will be consistent data types for all conditions thus maintaining symmetry.
I see nothing that would prevent you from doing this that would violate any conditions placed on the code by the author; the submission seems silent on the point.
  2 Comments
AmelieH
AmelieH on 22 Jul 2018
Thanks, this really is the best decision. Makes everything else easy to deal with.
dpb
dpb on 22 Jul 2018
Indeed, why the author chose to do that and break symmetry is bad design choice imo...I complain to TMW a lot about their penchant to do similar things in and between related functions! :)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!