フィルターのクリア

replace values in an array based on previous values

3 ビュー (過去 30 日間)
Vanessa
Vanessa 2017 年 7 月 27 日
コメント済み: Vanessa 2017 年 7 月 27 日
Hello everyone!!
I have a dataset with two columns.
  1. TEL_TYPE which take values 'P','A','D','N'
  2. PORT_FLAG which take values 'N','S'.
I have the below sequence which being repeated (not with steady number of 'P' in the middle)
TEL_TYPE : A P P P P D
PORT_FLAG : N N N S N N
I want to find when the PORT_FLAG='S' and then the PORT_FLAG where TEL_TYPE='D' to have the value 'S' so in the above example the result will be:
TEL_TYPE : A P P P P D
PORT_FLAG : N N N S N s
How can I achieve this? Thank you!
Vanessa
  1 件のコメント
Adam
Adam 2017 年 7 月 27 日
What is the purpose of finding the PORT_FLAG == 'S'? Are there occurrences of 'D' in TEL_TYPE that you don't want to change PORT_FLAG to 'S' for?

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

回答 (1 件)

Image Analyst
Image Analyst 2017 年 7 月 27 日
Here's one way, assuming they're character arrays:
TEL_TYPE = ['A' 'P' 'P' 'P' 'P' 'D' 'P' 'D' 'P' 'P']
PORT_FLAG = ['N' 'N' 'N' 'S' 'N' 'N' 'S' 'N' 'N' 'S']
% Find 'S's
sLocations = find(PORT_FLAG == 'S')
for k = 1 : length(sLocations)
index = sLocations(k);
firstDLocation = index + find(TEL_TYPE(index:end) == 'D', 1, 'first') - 1
if ~isempty(firstDLocation)
PORT_FLAG(firstDLocation) = 's'
end
end
PORT_FLAG % Display in command window.
Note I lengthened the arrays to make sure it worked for not only the first instance of S.
  1 件のコメント
Vanessa
Vanessa 2017 年 7 月 27 日
Thank you very much!!

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by