Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Assuming i have an output ("myArray") which is a 1x4 cell array and i also have a while loop within which i call the function that produces a new result which is of the same size and variable as my original array.

How do i append the new result into the existing one such that i still have a 1x4 array with the new results updated to the existing cells.

See an example code below:

% image
SegI = [2 2 2 0 0
        2 2 2 0 0
        1 1 2 0 0
        1 1 1 1 0
        1 1 1 1 1];

figure, imshow(SegI, [], 'InitialMagnification','fit')     
grid on; 

% face vectors obtained from each internal node in the image; such that edge %directions of 1|2 are denoted as 'm', and 0|2 as 'n', and others as NaN
% i made the 'b' up just for the purpose of detecting the output easily
allfaces = {NaN,NaN,NaN,NaN; NaN,NaN,NaN,NaN; 'm',NaN,'m',NaN; NaN,NaN,NaN,NaN; NaN,NaN,'n','n'; NaN,'n',NaN,'n'; NaN,'m',NaN,NaN; NaN,NaN,NaN,'m'; NaN,NaN,NaN,NaN; NaN,'m','b',NaN; 'm',NaN,NaN,'n';  NaN,NaN,NaN,NaN; NaN,NaN,NaN,NaN; NaN,NaN,NaN,NaN; NaN,NaN,NaN,NaN; NaN,NaN,NaN,NaN};


% find point vertices... which are nodes that contain pixel edges of both 1|2 and %0|2
Pvertex = find( any(strcmp('m', allfaces),2) & any(strcmp('n', allfaces),2) );


% create storage cell array as well as initialize Pvertex to inodej
myArray = cell(numel(Pvertex), 4);
inodej  = Pvertex;
cp = [3,3];     %coordinates of Pvertex


for k = 1: numel(Pvertex)      %loop through all Pvertices

    % find next node from Pvertex
     [myArray, allfaces, inodej, ynode, xnode, dist_node] = Obtain_nodes(k, cp, inodej, myArray, allfaces);


    while dist_node < sqrt(2)
        % find nodes until exit loop
       [myArray, allfaces, inodej, ynode, xnode, dist_node] = Obtain_nodes(k, cp, inodej, myArray, allfaces);
    end


end



The function is given as:

function [myArray, allfaces, inodej, ynode, xnode, dist_node] = Obtain_nodes(k, cp, inodej, myArray, allfaces)
% This function computes new nodes from previous ones, as well as
% store each results in the storage cell "myArray" 


if ( find( strcmp('m', allfaces(inodej(k),:)) ) == 1 ) == true 
    % Find the node at the northern end of the face after inodej
    inodej(k,:) =  inodej(k,:) - 1;

    % extract the n, s, e, w faces from allfaces
    a = allfaces(inodej(k),:);

    % store the new results (exclude the existing result at the southern face)
    a(1) = {NaN};
    myArray(k,:) = a;

    % Obtain a new allfaces that deals the South face for the current node  
    allfaces(inodej(k),:) = a;


elseif ( find( strcmp('m', allfaces(inodej(k),:)) ) == 2 ) == true 
    % Find the node at the southern end of the face  after inodej
    inodej(k,:) = inodej(k,:) - 4;

    % extract n, s, e, w faces from allfaces
    a = allfaces(inodej(k),:);

    % store the new results (exclude the node at the northern face)
    a(2) = {NaN};
    myArray(k,:) = a;


    % Obtain a new allfaces that deals the North face for the current node
    allfaces(inodej(k),:) = a;

end



The first function call in the for loop returns:

inodej = 10
, and
myArray = {NaN, 'm', 'b', NaN}


The function call within the while loop returns:

inodej = 6
, which draws a result from 6th row of allfaces to give:
myArray = {NaN, NaN, NaN, 'n'}


However, i do not get to see my initial (1st) result after the while loop exits because the new(2nd) result updates the cell array. Hence, the issue for me is how to get any new results stored and updated with the previous as the while loop is executed.

I would expect my final output to be in this form:

[{NaN, NaN}] [{'m', NaN}] [{'b', NaN}] [{NaN, 'n'}]



[^]



Please also bear in mind that I could for instance increase the search distance (dist_node) in which case i wouldn't know beforehand (except by visual inspection) how many new results i could get. Thus, the code will have to be able to append as many new results as obtainable.

Please i need any help/suggestions/advice to get through this. Thank you in advance.

What I have tried:

I have added an example code in the description.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900