フィルターのクリア

Replace array elements with another small array using indexing

3 ビュー (過去 30 日間)
TEJAS PHUTANE
TEJAS PHUTANE 2020 年 5 月 19 日
コメント済み: TEJAS PHUTANE 2020 年 5 月 19 日
Hi everyone,how to replace array with another small array using indexing
a=[1 1 4 1 3 3 1 1 1 3 4 1 0 0 0 0 0 0 0 0 0];
%replace 1 4 1 with 0 3 0 and 3 4 1 with 2 and b=b(b~=0); to get a new vector
b=[1 3 3 3 1 1 1 2];
Thank you.
  3 件のコメント
TEJAS PHUTANE
TEJAS PHUTANE 2020 年 5 月 19 日
I want to do it both ways, replace with 3 or with 0 3 0 and use b=b(b~=0); to get a new array without 0.
TEJAS PHUTANE
TEJAS PHUTANE 2020 年 5 月 19 日
%my code:
path=[1 1 4 1 3 3 1 1 1 3 4 1 0 0 0 0 0 0 0 0];
p=path;
n=length(p);
for i=1;i=n-3;i=i+1;
if ( p(i)==1 && p(i+1)==4 && p(i+2)==1)
p(i:i+2)= [0 3 0];
end
end
p=p(p~=0);
still not getting desired output

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

回答 (1 件)

Matt J
Matt J 2020 年 5 月 19 日
b=strrep( strrep(a,[1 4 1],3) ,[3 4 1],2)
  2 件のコメント
TEJAS PHUTANE
TEJAS PHUTANE 2020 年 5 月 19 日
this actually works but a warning occurs:Inputs must be character vectors, cell arrays of character vectors, or string arrays.
TEJAS PHUTANE
TEJAS PHUTANE 2020 年 5 月 19 日
if instead of integers i have character vector like
path=["L" "L" "U" "L" "S" "S" "L" "L" "L" "S" "U" "L" "0" "0" "0" "0" "0" "0" "0" "0"];
how can i get shortest path (for line maze) eg: replace L U L with S and S U L with R.
shortpath=["L" "S" "S" "S" "L" "L" "L" "R"];

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

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by