Azzera filtri
Azzera filtri

Info

Questa domanda è chiusa. Riaprila per modificarla o per rispondere.

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

1 visualizzazione (ultimi 30 giorni)
Clayton
Clayton il 15 Ott 2014
Chiuso: MATLAB Answer Bot il 20 Ago 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

Risposte (1)

Image Analyst
Image Analyst il 15 Ott 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.

Questa domanda è chiusa.

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by