Using indices of array to define indices of cell array

1 view (last 30 days)
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.

Accepted Answer

Walter Roberson
Walter Roberson on 1 Feb 2012
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.

More Answers (1)

Honglei Chen
Honglei Chen on 1 Feb 2012
Similar to what you did for vectors:
C{A>3}

Community Treasure Hunt

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

Start Hunting!