Borrar filtros
Borrar filtros

Creating a function for a two-dimensional array

11 visualizaciones (últimos 30 días)
amateurintraining
amateurintraining el 13 de Oct. de 2017
Respondida: Walter Roberson el 13 de Oct. de 2017
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?

Respuestas (1)

Walter Roberson
Walter Roberson el 13 de Oct. de 2017
find() returns column vectors, and [false, column_vector] is going to fail. [false; column_vector] would work.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by