假设我有一个1x4的单元格数组,并且我还有一个while loop
,我在其中调用产生与原始数组大小相同的新结果的函数。
如何将新结果附加到现有结果中,以便我仍然拥有1x4数组,并将新结果附加到现有单元格中。
请参阅下面的示例代码:
% 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)
% 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
该功能如下:
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
%# compute the distance between inodej and Pvertex
% firstly get the coordinates of the inodej
xnode(k) = mod(inodej(k,:) - 1, 4) + 1;
ynode(k) = (inodej(k,:) - xnode(k))/4 + 1;
% Then find the distance
dist_node(k,:) = sqrt( (cp(1) - ynode(k)).^2 + (cp(2) - xnode(k)).^2 );
end
有关一些解释,请参见下图。
for loop
中的第一个函数调用返回:
inodej = 10
和myArray = {NaN, 'm', 'b', NaN}
while loop
内的函数调用返回:
inodej = 6
,它从allfaces
的第6行中提取结果,以便:
myArray = {NaN, NaN, NaN, 'n'}
但是,我不会在while loop
退出后看到我的初始(第一个)结果,因为新的(第二个)结果更新了单元格数组。因此,对我而言,问题是如何在执行while loop
时使用之前的内容存储和更新任何新结果。
我希望我的最终输出是这种形式的东西:
[{NaN, NaN}] [{'m', NaN}] [{'b', NaN}] [{NaN, 'n'}]
请注意,我可以增加搜索距离(dist_node)
,在这种情况下我不会事先知道(除了目视检查)我可以获得多少新结果。因此,代码必须能够附加尽可能多的新结果。
请,我需要任何帮助/建议/建议才能完成此操作。提前谢谢。
答案 0 :(得分:0)
myArray1 = {NaN, 'm', 'b', NaN};
myArray2 = {NaN, NaN, NaN, 'n'};
result = cellfun(@(x,y) {x y}, myArray1, myArray2, 'UniformOutput', false)'
x:myArray1的每个元素
y:myArray2的每个元素
UniformOutput = false确保输出为单元格。
最后的素数将行向量转换为列向量。