Is there a more efficient way to loop this?

1 回表示 (過去 30 日間)
Or Shem Tov
Or Shem Tov 2020 年 3 月 22 日
コメント済み: Walter Roberson 2020 年 3 月 23 日
Hi guys,
I created a code that extracts certain values from a text headline, each one of these values appear in a different list as follows:
Is there a way to write this code more efficiently without having to go through 5 for loops?
the loops run: 1) 133 times 2) 11 times 3) 81 times 4) 32 times 5) 14 times
Big thanks to anybody that helps!
% Find the issuer
for k = 1:numel(issuersList)
% Check to see which text(s) contain this issuer.
ind = contains(Text, issuersList(k));
% Assign this issue to the correct rows in the output table.
Issuer(ind) = issuersList(k);
end
% Find the action taken by the issuer
for k = 1:numel(ratingActionList)
% Check to see which text(s) contain this issuer.
ind = contains(Text, ratingActionList(k));
% Assign this issue to the correct rows in the output table.
RatingAction(ind) = ratingActionList(k);
end
% Find the new rating
for k = 1:numel(newratingList)
% Check to see which text(s) contain this new rating.
ind = contains(Text, newratingList(k));
% Assign this issue to the correct rows in the output table.
newRating(ind) = newratingList(k);
end
% Find the old rating
for k = 1:numel(oldRatingsList)
% Check to see which text(s) contain this old rating.
ind = contains(Text, oldRatingsList(k));
% Assign this issue to the correct rows in the output table.
oldRating(ind) = oldRatingsList(k);
end
% Find the price target action
for k = 1:numel(ptActionsList)
% Check to see which text(s) contain this price target action.
ind = contains(Text, ptActionsList(k));
% Assign this issue to the correct rows in the output table.
PTAction(ind) = ptActionsList(k);
end
  3 件のコメント
Walter Roberson
Walter Roberson 2020 年 3 月 23 日
What is the datatype of Text? What is the datatype of issuersList and other variables that are being searched for?
Or Shem Tov
Or Shem Tov 2020 年 3 月 23 日
They are all string arrays as such:
issuersList =
135×1 string array
"jp morgan"
"citigroup"
"credit suisse"
"oppenheimer"
"morgan stanley"
...
...
...

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

回答 (1 件)

Walter Roberson
Walter Roberson 2020 年 3 月 23 日
Issuers = regexp(Text, strjoin(issuersList, '|'),'match','once');
  2 件のコメント
Or Shem Tov
Or Shem Tov 2020 年 3 月 23 日
Perfect! THANKS!
Walter Roberson
Walter Roberson 2020 年 3 月 23 日
Note: this relies on Text being a string array or a cell array of character vectors. In particular if Text is a character vector, then it will only find the first match, including for the case where Text is a character vector with embedded newline characters. If you happen to have a character vector with embedded newlines and want to find the issuer for each line, then use
regexp(Text, '\n', 'split')
before doing the above regexp()

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

カテゴリ

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

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by