フィルターのクリア

How do I find the rows in one table that have certain string?

120 ビュー (過去 30 日間)
Rafael Borobio Castillo
Rafael Borobio Castillo 2023 年 8 月 1 日
編集済み: dpb 2023 年 8 月 1 日
I have a Table with 80,000 rows and 19 variables lets call it T, in column 17 I have the STATUS of the products, i.e., 'Active', 'Inactive', or 'Other'.
I want to identify the rows in column 17 that contain 'Inactive' so I can eventually delete these form the original table.
I have used the following code but it separates the rows containing 'Inactive' it does not identify the row number
[rows]=T(strcmp(T.STATUS,'Inactive'),:);

採用された回答

Voss
Voss 2023 年 8 月 1 日
編集済み: Voss 2023 年 8 月 1 日
"I want to identify the rows in column 17 that contain 'Inactive'"
% get the indicies of rows with "Inactive" STATUS:
inactive_rows = find(strcmp(T.STATUS,'Inactive'));
"so I can eventually delete these form the original table"
% delete them from T:
T(inactive_rows,:) = [];
  1 件のコメント
Voss
Voss 2023 年 8 月 1 日
編集済み: Voss 2023 年 8 月 1 日
Of course it's better to use logical indexing instead of find, like dpb says in his answer, but if you want to see the actual row numbers, use find.

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

その他の回答 (1 件)

dpb
dpb 2023 年 8 月 1 日
T.STATUS=categorical(T.STATUS); % more efficient for categorical-type variables
isInact=(T.STATUS=="Inactive"); % logical vector of inactive
Use the far more efficient logical indexing vector; you don't need to find() the row number explicitly at all for this purpose. It's not terribly often that actually do need to have the row number itself although find will do so, avoid using it until you do find (so to speak) a real problem you can't solve otherwise.
  2 件のコメント
Rafael Borobio Castillo
Rafael Borobio Castillo 2023 年 8 月 1 日
But later how do you delete the rows that are catalogued as Inactive?
dpb
dpb 2023 年 8 月 1 日
編集済み: dpb 2023 年 8 月 1 日
Use the logical addressing vector, of course...two choices, either
T.STATUS(isInact,:)=[];
delete the inactive rows, or
T=T(T.STATUS(~isInact,:));
keep everything that is NOT inactive...
If this is new to you, see <array-indexing> in the doc; some time spent in the <Getting Started> language fundamentals section on basic MATLAB syntax and operations would provide a pretty high return on the investment. There's also the OnRamp video course if you're more attuned to that sort of thing.

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

カテゴリ

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

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by