Filter löschen
Filter löschen

Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

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

1 Ansicht (letzte 30 Tage)
Clayton
Clayton am 15 Okt. 2014
Geschlossen: MATLAB Answer Bot am 20 Aug. 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

Antworten (1)

Image Analyst
Image Analyst am 15 Okt. 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.

Diese Frage ist geschlossen.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by