Creating a function for a two-dimensional array
19 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Walter Roberson
on 13 Oct 2017
find() returns column vectors, and [false, column_vector] is going to fail. [false; column_vector] would work.
0 Comments
See Also
Categories
Find more on Systems of Nonlinear Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!