Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

How do i skip portions of a loop after getting my answer?

1 visualización (últimos 30 días)
Clayton
Clayton el 15 de Oct. de 2014
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
I am trying to make a loop that modifies a certain value in a matrix and skip to the end of the loop after the modification is made. Right now the value will be modified, but is modified again before the loop can end and I don't know how to fix it.
world = ['#' '.' '.' '.' '.' '.' '#' '.' 'v' '#'
'.' '#' '.' '#' '#' '.' '.' '.' '.' '.'
'.' '.' '#' '#' '.' '.' '#' '#' '.' '#'
'#' '.' '.' '.' '.' '#' '.' '.' '.' '.'
'.' '#' '.' '#' '#' '.' '.' '#' '.' '.'
'.' '#' '.' '.' '.' '#' '#' '#' '.' '#'
'#' '.' '#' '.' '.' '#' '#' '#' '.' '.'
'.' '#' '.' '#' '.' '.' '.' '#' '.' '.'
'.' '.' '.' '#' '#' '.' '#' '.' '#' '.'
'G' '#' '.' '.' '.' '.' '.' '.' '#' '#']
idx = 81; while idx ~= 10; move = input('Your move?(a,w,d,q)','s'); for idx = find(world == 'v'); switch(move) case 'a' world(idx) = '>'; case 'w' [r,c] = find(world == 'v'); world(r,c) = '.'; world(r+1,c) = 'v'; case 'd' world(idx) = '<'; end end for idx1 = find(world == '^'); switch(move) case 'a' world(idx1) = '<'; case 'w' [r,c] = find(world == '^'); world(r,c) = '.'; world(r-1,c) = '^'; case 'd' world(idx1) = '>'; end end for idx2 = find(world == '<'); switch(move) case 'a' world(idx2) = 'v'; case 'w' [r,c] = find(world == '<'); world(r,c) = '.'; world(r,c-1) = '<'; case 'd' world(idx2) = '^'; end end for idx3 = find(world == '>'); switch(move) case 'a' world(idx3) = '^'; case 'w' [r,c] = find(world == '>'); world(r,c) = '.'; world(r,c+1) = '>'; case 'd' world(idx3) = 'v'; end end clc; disp(world); counter = counter + 1

Respuestas (1)

Image Analyst
Image Analyst el 15 de Oct. de 2014
I didn't really read/understand that code, but in general to skip portions of code you can use "if", break, or continue commands.

La pregunta está cerrada.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by