Questions tagged [cell-array]

In MATLAB cell arrays are a built-in container class allowing for the storage of different types of data in each "cell" of the array.

A cell array is a data type with indexed data containers called cells, where each cell can contain any type of data. Cell arrays commonly contain either lists of text strings, combinations of text and numbers, or numeric arrays of different sizes.

You can refer to subsets of cells by enclosing indices in smooth parentheses, (). The cell contents can be accessed by indexing with curly braces, {}.

Examples:

>> ca = cell(2, 3);        % create 2-by-3 cell array with empty cells
>> ca{1, 1} = 3;           % store a double scalar in one cell
>> ca{1, 2} = rand(3, 4);  % store an entire matrix into a cell
>> ca{1, 3} = 'will you look at that, a string in a cell!';
>> ca{2, 1} = @mean;       % a function handle can also be stored in a cell
>> ca{2, 3} = uint32(45);  % store a uint32 scalar
>> ca(1, 1:3)   % Get a subset of cells (first row)

ans =

  1×3 cell array

    [3]    [3×4 double]    'will you look at that, a string in a cell!'

>> ca{1, 2}     % Get the contents of a cell

ans =

    0.8147    0.9134    0.2785    0.9649
    0.9058    0.6324    0.5469    0.1576
    0.1270    0.0975    0.9575    0.9706
1035 questions
6
votes
2 answers

MATLAB: index a cell array with cell array of arrays and return a cell array

Say I have a cell array of (n X 1) vectors, A, and a cell array of vectors containing indices into A, called B. I wish to extract a cell array, C, such that C{i} = [A{B{i}}]. In other words, I have a cell array of arrays of indices, and I want to…
reve_etrange
  • 2,561
  • 1
  • 22
  • 36
6
votes
2 answers

Conditional element replacement using cellfun

vec = randi(10,10,1) vec(vec < 5) = 0 func = @(x) x(x < 5) = 0 % This isn't valid How am I supposed to translate the second line of code into a function handle that I can use in conjunction with cellfun?
6
votes
1 answer

How to remove last element from cell array in Matlab?

How to remove last element from cell array in Matlab? Documented method does not work: >> A = {'a', 'b', 'c'} A = 1×3 cell array 'a' 'b' 'c' >> A{end}=[] A = 1×3 cell array 'a' 'b' [] I need array become 1x2.
Dims
  • 47,675
  • 117
  • 331
  • 600
6
votes
5 answers

Fastest way to get class types of elements of a cell array

I have a (large) cell array, with various data types. For example, myCell = { 1, 2, 3, 'test', 1 , 'abc'; 4, 5, 6, 'foob', 'a', 'def' }; This can include more obscure types like java.awt.Color objects. I want to ensure that the data…
Wolfie
  • 27,562
  • 7
  • 28
  • 55
6
votes
1 answer

Random selection of a member's location in a nested cell of cells: Matlab

I have a nested cell of cells like the one below: CellArray={1,1,1,{1,1,1,{1,1,{1,{1 1 1 1 1 1 1 1}, 1,1},1,1},1,1,1},1,1,1,{1,1,1,1}}; I need to randomly pick a location in CellArray. All members' locations of CellArray must have same chances to be…
Zander
  • 167
  • 1
  • 10
6
votes
2 answers

How can I divide a matrix into unequally-sized submatrices?

I am wondering if it is possible to use the mat2cell function to divide an MxN matrix into 10 submatrices with the same column size, N, and approximately the same row size ~M/10? If mod(M, 10) == 0 then all submatrices will have the same size,…
C. Reed
  • 2,382
  • 7
  • 30
  • 35
6
votes
4 answers

Strcmp for cell arrays of unequal length in MATLAB

Is there an easy way to find a smaller cell array of strings within a larger one? I've got two lists, one with unique elements, and one with repeating elements. I want to find whole occurrences of the specific pattern of the smaller array within the…
Doresoom
  • 7,398
  • 14
  • 47
  • 61
6
votes
2 answers

How can I access nested cell arrays in MATLAB?

I would like to make a nested cell array as follows: tag = {'slot1'} info = {' name' 'number' 'IDnum'} x = {tag , info} And I want to be able to call x(tag(1)) and have it display 'slot1'. Instead I am getting this error: ??? Error using ==>…
Ben Fossen
  • 1,331
  • 6
  • 21
  • 33
6
votes
4 answers

Substrings from a Cell Array in Matlab

I'm new to MATLAB and I'm struggling to comprehend the subtleties between array-wise and element wise operations. I'm working with a large dataset and I've found the simplest methods aren't always the fastest. I have a very large Cell Array of…
fodfish
  • 85
  • 1
  • 4
5
votes
2 answers

Concatenate two strings where first string has a space in the end Matlab

I'm trying to concatenate two strings using: str=strcat('Hello World ',char(hi)); where hi is a 1x1 cell which has the string 'hi'. But str appears like this Hello Worldhi. Why am i missing a '' after Hello World?
Tak
  • 3,536
  • 11
  • 51
  • 93
5
votes
1 answer

How to sort a structure array

How do I sort the oo structure array alphabetical order by item name. oo = struct('Item', {'Quill','Ink Pen', 'Pencil'}, 'Cost', {10, 2, 1}) I tried using the sort() function but it didn't work? Thank you.
Jason Thapa
  • 123
  • 9
5
votes
3 answers

Count unique rows in a cell full of vectors

I have a cell in MATLAB where each element contains a vector of a different length e.g. C = {[1 2 3], [2 4 5 6], [1 2 3], [6 4], [7 6 4 3], [4 6], [6 4]} As you can see, some of the the vectors are repeated, others are unique. I want to count the…
Mark
  • 1,277
  • 3
  • 13
  • 27
5
votes
2 answers

Is there an idiomatic way to exchange two elements in a cell array?

I know that I can write it like this: tmp = arr{i} arr{i} = arr{j} arr{j} = tmp But is there a simpler way? For instance, in Python I'd write: arr[i], arr[j] = arr[j], arr[i]
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
5
votes
1 answer

matlab char array to cell array

say I have an array of chars which looks like.... hello hillo hello and I would like to convert them to a cell array which would be the same as... A = {'hello';'hillo';'hello'} how would I go about doing this, I've tried using…
bdavies6086
  • 382
  • 1
  • 5
  • 19
5
votes
3 answers

When to use a cell, matrix, or table in Matlab

I am fairly new to matlab and I am trying to figure out when it is best to use cells, tables, or matrixes to store sets of data and then work with the data. What I want is to store data that has multiple lines that include strings and numbers and…
Austen Novis
  • 444
  • 1
  • 12
  • 30
1 2
3
68 69