Using indices of array to define indices of cell array

If I am wanting to define certain elements in an array based on the indices of another array that meet a certain condition, I know how to do so. For example, if I have an array A, and I want to create an array B in which I define the elements of B that match those of A for which A>3 one way and the rest another, I know to do this:
B(A>3) = %definition
B(A<=3) = %other definition
However, what I am wanting to do is something similar, but for cell arrays. So, to use the same example, I want to define cells of C with the same indices as those of A that meet my condition, A>3. Is there a simple way to do so if loops need to be avoided? Note that A would still be an array in the scenario.

 채택된 답변

Walter Roberson
Walter Roberson 2012년 2월 1일
C(A>3) = a cell array of the appropriate length
OR
[C{A>3}] = deal(a sequence of the appropriate length)
OR
[C{A>3}] = a cell or array sequence of the appropriate length
In most cases you want to do a cell-to-cell assignment such as
C(A>3) = D(A>3);
An example of an array sequence of the appropriate length would be
fileinfo = dir('*.jpg');
A = [fileinfo.size];
[C{A>3}] = fileinfo(A>3).name;
Using C{A>3} on the left side of an assignment statement without the [] around it, is going to fail unless there is exactly one selected cell.

추가 답변 (1개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by