extracting index number if an item exist in some columns while not in the rest columns

조회 수: 1 (최근 30일)
Hi,
let us say, i have a data table which has following form:
Index name A1 A2 A3,,,,,A10
and there is an item called David. The interesting things is David is in some of the columns (david is not A1, A2 columns and is in the rest of the columns).
I am interesed in the extracting the index number of david whether is in column or not (if not in the column, may return o or NaN), i tried hard, but could not write the corresponding code, can some one help?
Thanks.
  댓글 수: 4
mirewuti muhetaer
mirewuti muhetaer 2020년 3월 27일
Hi, @Waler:
it appears at most once in each column.
Thanks.

댓글을 달려면 로그인하십시오.

채택된 답변

Akira Agata
Akira Agata 2020년 3월 27일
OK. Then, the solution would be like this:
% Sample table
Name = {'David','Bowie','Lynch','Mirewuti','Muhetaer','Akira','Agata'};
rng('default'); % for reproducability
T = table(Name(randperm(7,6))',Name(randperm(7,6))',Name(randperm(7,6))',Name(randperm(7,6))',...
'VariableNames',{'A1','A2','A3','A4'});
% Find the column which contains 'David'
idx = strcmp(T{:,:},'David');
idx = any(idx);
% Return the column name
colName = T.Properties.VariableNames(idx);
[Sample Table]
>> T
T =
6×4 table
A1 A2 A3 A4
____________ ____________ ____________ ____________
{'Lynch' } {'Akira' } {'Muhetaer'} {'Lynch' }
{'Bowie' } {'Bowie' } {'David' } {'Mirewuti'}
{'Mirewuti'} {'Mirewuti'} {'Bowie' } {'Akira' }
{'David' } {'Muhetaer'} {'Lynch' } {'Agata' }
{'Akira' } {'Lynch' } {'Akira' } {'David' }
{'Agata' } {'Agata' } {'Mirewuti'} {'Muhetaer'}
[Extracted column name]
>> colName
colName =
1×3 cell array
{'A1'} {'A3'} {'A4'}

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by