MATLAB Answers

How to delete specific cells according to the condition from cell array?

조회 수: 54(최근 30일)
CheshireM
CheshireM 2021년 9월 23일 22:27
댓글: the cyclist 2021년 9월 29일 0:47
I have a cell array test (4x189).
Each cell is a double array of different sizes. I want to delete all the cells, that look like this (basically, they are all 7x4, the same looking, but different numbers):
NaN 1 0.437500000000000 0
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN 8 NaN NaN
NaN 719 NaN NaN
NaN 719 NaN NaN
NaN NaN NaN NaN
Could you, please, help me to do that?
Thank you so much!
  댓글 수: 3
CheshireM
CheshireM 2021년 9월 23일 23:07
Basically, I am trying to solve the problem of removing sheets from Excel file that satisfy specific condition (B6=B7).

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

채택된 답변

the cyclist
the cyclist 2021년 9월 24일 13:28
This line of code will identify the cell locations that have a 7x4 array, having those two elements equal:
deleteIdx = cellfun(@(x)(x(5,2)==x(6,2))&&all(size(x)==[7,4]),test);
It's still not perfectly clear to me what you prefer as output, but maybe this is good enough.
  댓글 수: 6
the cyclist
the cyclist 2021년 9월 29일 0:47
Did you do the preallocation step?
output = cell(size(test));
Also, you said you wanted empty cells, but now you say you want NaN. Which is it?
Can you upload your 4x189 array, to work on directly?

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

추가 답변(1개)

Chunru
Chunru 2021년 9월 24일 1:59
If you want to keep the output as a cell array (as the input), you cannot delete them, but you can assign them to empty array.
for j=1:189
for i=1:4
if all(size(test{i,j})==[7 4]) && test{i,j}(5,2)==test{i,j}(6,2)
test{i,j} = [];
end
end
end

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by