Index a cell array with an cell

Hi all
I have a cell array
C {1x2}
C{1} 1x180 values
C{2} 1x1260 values
and B with the same size containing zeros and ones. Now I want to do something:
C{B}=[]; to delete all entries of C where B contains a one whearas if B contains a zero all values should be maintained.
How can i do this?
Thank you!

답변 (1개)

Jorg Woehl
Jorg Woehl 2021년 3월 4일

0 개 추천

Hi Christian,
You can pick the corresponding entries for each of the cells of C as follows:
C{i}(B{i}==0)
The results are then easily combined into a new cell array, as shown in this short example:
C = {[1 2 3], [10 11 12 13 14 15 16]}
B = {[0 1 1], [ 0 1 0 0 1 1 0]}
newC = {C{1}(B{1}==0), C{2}(B{2}==0)}
The newC cell array now contains {[1], [10 12 13 16]}.

댓글 수: 4

CSCh
CSCh 2021년 3월 4일
Hi Jorg,
thank you for your answer.
With that: C{i}(B{i}==0), your example works. Howerver, for me
i get the following error:
binary operator '==' not implemented for 'cell' by 'scalar' operations.
Maybe it has something to do with my cell structure? here is an abstract from C
C{1}(1:2) =
{
[1,1] =
{
[1,1] = 0.40
}
[2,1] =
{
[1,1] = 0.70
}
}
....
My B is B{1}(1:2)
[1,1] = 0
[1,2] = 0
}
...
Could you send a command for building (shortened) examples for the cell arrays C and B, like in my code above? Sending the output from
C
B
may also be helpful, as a start.
I am still not sure how your cell arrays are structured; it looks like C may be a 1-by-2 (or 2-by-1?) array of cells, each containing a cell array of scalars... but whatever I tried, I don't get the same error you are referring to. An example would make it clear what we are dealing with.
CSCh
CSCh 2021년 3월 8일
Joerg, many thanks for your effort and your help. I don't know how to create such a cell array. To don't loss so much time, I made a work around with a loop (which I actually wanted to avoid). Thank you
Jorg Woehl
Jorg Woehl 2021년 3월 8일
Sounds good - cell arrays can indeed be tricky to create and work with. Thanks for the follow-up.

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

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

릴리스

R2020a

질문:

2021년 3월 3일

댓글:

2021년 3월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by