Creating a function for a two-dimensional array
19 次查看(过去 30 天)
显示 更早的评论
I have a function:
function [ filled ] = travelDistance( blank )
%TRAVELDISTANCE
% blank: two-dimensional array comprised of -1s, 0s, and 1s
% filled: blank that is modified (replace every 0 in blank with its
% distance to the nearest 1, tarting at 2, traveling along cardinal
% directions without passing through a -1 value)
function d = xyspace(A,x,y)
idx1=find(ismember(A,x));
idx2=find(ismember(A,y));
x=union(idx1,idx2);
whichset=ismember(x,idx1)+2*ismember(x,idx2);
idx=diff(whichset)>0;
d=x([false,idx])-x([idx,false]);
end
filled=xyspace(blank,0,1);
end
but this function doesn't work on two-dimensional arrays and receives an error:
Error using horzcat
Dimensions of matrices being concatenated are
not consistent.
Error in travelDistance/xyspace (line 14)
d=x([false,idx])-x([idx,false]);
Error in travelDistance (line 17)
filled=xyspace(blank,0,1);
How do I write the function to apply to 2x2 arrays?
0 个评论
回答(1 个)
Walter Roberson
2017-10-13
find() returns column vectors, and [false, column_vector] is going to fail. [false; column_vector] would work.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Systems of Nonlinear Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!