for loops question, cell array, cardinal direction
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Question:
Finding the shortest distance between two “points” is a class of algorithm called pathfinding. The distancemay be a physical distance between two locations (e.g. driving directions), or something more abstractlike the fastest sequence from start to finish of a construction project. Here you will consider a simplified pathfinding problem where the distance is indices in a matrix. Starting with a matrix of -1s, 0s and 1s, find the distance of every 0-valued entry to the nearest 1-valuedentry, without passing through -1s. For simplicity, being directly next to a 1 is considered a distance of 2. Create a function with the following header:
function [ filled ] = travelDistance( blank )
where: blank is a two-dimensional array comprised of -1s, 0s and 1s, and filled is blank modified to the specifications below. To create filled, replace every 0 in blank with its distance to the nearest 1 (staring at 2), traveling along cardinal directions (up, down, left and right) without passing through a -1 value. That is, all 0s that ared irectly next to a 1 should be changed to a 2 and all 0s next to any new 2s should be changed to 3s, and soon. Spaces with -1 values should be treated as “walls” and should stay -1. If there is no route from a 0 to a 1 without passing through a -1, it should remain a 0. The ultimate result should be that every 0 that is connected to a 1 is replaced with its distance to the nearest 1. For simplicity you may assume that the edges of blank always contain a -1. Your function should be able to reproduce the following test case:
>> m_smallm_small =-1 -1 -1 -1 -1 -1 -1-1 0 0 -1 0 0 -1-1 0 1 -1 0 -1 -1-1 0 -1 -1 -1 -1 -1-1 0 -1 0 1 0 -1-1 -1 -1 -1 -1 -1 -1
>> travelDistance(m_small)
ans =-1 -1 -1 -1 -1 -1 -1-1 3 2 -1 0 0 -1-1 2 1 -1 0 -1 -1-1 3 -1 -1 -1 -1 -1-1 4 -1 2 1 2 -1-1 -1 -1 -1 -1 -1 -1
My answer:
function [filled] = travelDistance( blank )
% blank is a two-dimensional array comprised of -1s, 0s and 1s,
% and filled is blank modified to the specifications below.
% replace every 0 in blank with its distance to the nearest 1(starting at 2),
% all 0s that are directly next to a 1 should be changed to a 2
% all 0s next to any new 2s should be changed to 3s.
% -1 stay -1
filled = blank;
[a,b] = size(blank);
for i=1:1000
for X = 1:a
for Y = 1:b
for filled(X,Y)==0;
for filled(X+1,Y)==i||filled(X-1,Y)==i||filled(X,Y+1)==i||filled(X,Y-1)==i
filled(X,Y)=i+1;
end
else
filled = blank;
end
end
end
just wondering which step that I missed
0 commentaires
Réponses (1)
Walter Roberson
le 14 Oct 2017
"for" must be followed by a (unindexed and unqualified) variable name and then a "=" and then an expression .
for filled(X,Y)==0
is not valid syntax. Perhaps you wanted "if" instead of "for"
if filled(X,Y)==0
0 commentaires
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!