フィルターのクリア

Comparing elements between a character and a numerical column

1 回表示 (過去 30 日間)
Jasmine Karim
Jasmine Karim 2018 年 7 月 23 日
コメント済み: Jasmine Karim 2018 年 7 月 25 日
Not sure if this is written correctly. Suppose there is a cell array where the first column holds characters ie) 'con', 'func', key', and the second column holds numerical values ie) 74 85 98. For x = 1:length(Event) [where event is the array], if event{x,1} is 'con' AND the cell immediately after it (x+1) is 'key', take the horizontal values and place those in a new array. For example:
Column 1 = ['con', 'key', 'con', 'con', 'var', 'con', key'] Column 2 = [ 74 85 94 67 56 84 23]
The new array would look like Column 1 = ['con', 'key', 'con, 'key'] column 2 = [74 85 84 23]
for x = 1:length(event)
if event{x,1} == 'con' & event{x+1,1} == 'key'
iEvent = event{x,:}
end
end
One of the issues I think is that == does not work with characters?

採用された回答

Adam Danz
Adam Danz 2018 年 7 月 23 日
編集済み: Adam Danz 2018 年 7 月 23 日
Here ares some fake data using your pattern and a solution. The solution identifies the rows of 'data' where 'key' follows 'con' and puts all of those rows into a new variable, 'output'.
data = {
'con' 74;
'key' 85;
'con' 94;
'con' 67;
'var' 56;
'con' 84;
'key' 23
'con' 67;
'var' 56;
'con' 84;
'key' 23};
% row indices of con & key
isCon = strcmp(data(:,1), 'con');
isKey = strcmp(data(:,1), 'key');
% rows of 'key' where 'con' preceded
ConKeyIdx = find(isKey(2:end) & isCon(1:end-1));
ConKeySubs = sort([ConKeyIdx;ConKeyIdx+1]); %row numbers of con-key neighbors
output = data(ConKeySubs,:);
output =
{'con'} {[74]}
{'key'} {[85]}
{'con'} {[84]}
{'key'} {[23]}
{'con'} {[84]}
{'key'} {[23]}
  1 件のコメント
Jasmine Karim
Jasmine Karim 2018 年 7 月 25 日
Thank you this works great! I should have thought to use strcmp with characters...

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by