Creating a function for a two-dimensional array

조회 수: 19 (최근 30일)
amateurintraining
amateurintraining 2017년 10월 13일
답변: Walter Roberson 2017년 10월 13일
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?

답변 (1개)

Walter Roberson
Walter Roberson 2017년 10월 13일
find() returns column vectors, and [false, column_vector] is going to fail. [false; column_vector] would work.

카테고리

Help CenterFile Exchange에서 Systems of Nonlinear Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by