Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 390
Dims Avatar asked Jan 25 '18 07:01

Dims


People also ask

How do I delete a set of cells in an array?

Delete sets of cells using standard array indexing with smooth parentheses, (). For example, remove the second row of C.

How to delete the contents of a particular cell in Excel?

Delete the contents of a particular cell by assigning an empty array to the cell, using curly braces for content indexing, {}. C= 3×3 cell array { [1]} { [ 2]} { [3]} { [4]} {0x0 double} { [6]} { [7]} { [ 8]} { [9]} Delete sets of cells using standard array indexing with smooth parentheses, (). For example, remove the second row of C.

How do you remove the first line of a matrix?

In a matrix, to remove the columns in which the element of the first line is 0, we can use: ind2remove = (A(1,:) == 0); A(:,ind2remove) = [];


1 Answers

You have to use round parentheses instead of curly braces (which act on the inner cell values and not on the cells themselves):

A(end) = [];

For more details, refer to the last section of the official documentation you linked:

https://mathworks.com/help/matlab/matlab_prog/delete-data-from-a-cell-array.html

like image 101
Tommaso Belluzzo Avatar answered Sep 29 '22 10:09

Tommaso Belluzzo