delete row from cell array under specific conditions

조회 수: 2 (최근 30일)
Luca Zinicola
Luca Zinicola 2018년 3월 10일
댓글: Von Duesenberg 2018년 3월 10일
Hi, sorry for the question, but I'm new at programming in Matlab.
I have a Cell Array, let's say CellArray, and I want to delete the rows where (the element in the first column is equal to a specified number) and (the element in the third column is equal to a string, for istance 'up') how do I do it? I think with cellfun I should be able to do it in only one line of code, but I don't know how.

답변 (1개)

Von Duesenberg
Von Duesenberg 2018년 3월 10일
Here's a quick made up example (if you can convert your cell to a table with cell2table); of course, it would have been easier if you had provided your actual data:
newT = table([1; 2; 3], {'is'; 'is'; 'is'}, {'up'; 'up'; 'down'})
newT(~strcmp('1', newT.Var1) & ~strcmp('up', newT.Var3),:)
  댓글 수: 2
Luca Zinicola
Luca Zinicola 2018년 3월 10일
편집: Luca Zinicola 2018년 3월 10일
yes, sorry, I know it'd be easier, but I can't show it since it's part of my research. Let's say I have this:
myCell = { 1, 'up', 15; 18, 'down', 20; 3, 'up', 15; 15, 'down', 100; 18, 'up', 17; 18, 'up', 120}
and I want to delete the rows where the first element is 18 and the second 'up', that is in this example the latter and the one before. Given the mixed structure, I adopted the Cell Array structure, and I cannot change it. I thought I should do something like:
idx = find(ismember ( cell2mat( myCell( : , 1)), 18));
idx2 = strcmp(myCell(:,2) , 'up');
condicio = intersect(idx, idx2);
myCell(condicio, :) = [];
So, is it right, or should I do something different? As I said, I must operate on Cell Array, I cannot change the structure because I can't change all the other lines of code because of this, and also because it's the most convenient structure for my data
Von Duesenberg
Von Duesenberg 2018년 3월 10일
I'm pretty sure somebody will come up with the right solution, but you can always use mine: convert your cell to table, delete the relevant rows, and convert the table back to cell (with table2cell)...

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

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by