フィルターのクリア

How do I replace a value in a matrix at a certain point?

2 ビュー (過去 30 日間)
Clayton
Clayton 2014 年 10 月 14 日
コメント済み: Stephen23 2014 年 10 月 14 日
I'm trying to identify where the certain character is in my matrix and replace that character based on user input at that location. I cannot figure out a command that will work but this is what I have so far.
move = input('Your move?(a,w,d,q)','s');
switch (move)
case 'a'
find(world == 'v')
[r,c] = find(world == 'v')
for world = 'v'
world(r,c) = '>'
end
for world = '^'
world(r,c) = '<'
end
for world = '<'
world(r,c) = 'v'
end
for world = '>'
world(r,c) = '^'
end

採用された回答

Guillaume
Guillaume 2014 年 10 月 14 日
I'm afraid the code you show makes no sense at all.
To find something in a matrix, you indeed use find. Once you've found where it is, it's a simple matter of indexing to put a new value there
idx = find(m == searchvalue); %don't use [r,c] if there's going to be more than one found value
m(idx) = newvalue;
Maybe, what you're trying to do is this?
idx = find(world == 'v');
switch(move)
case 'a'
world(idx) = '<';
case 'w'
world(idx) = '^';
case 's'
world(idx) = 'v';
case 'd'
world(idx) = '>';
end
  1 件のコメント
Stephen23
Stephen23 2014 年 10 月 14 日
A faster (and often neater) alternative to find is to simply to use logical indexing .

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by