This question already has an answer here:
Matlab - Delete row in Cell array if value contains xxx
(1 answer)
Closed 8 years ago.
A = {'A1'; 'A2'; 'A3'}
I need to find and delete row contain 'A2'(char)
Result:
A = 'A1'
'A3'
Thanks for your help!
A is not a matrix, it is a cell array.
So you may use cellfun to perform operations on cells. In your case, and in a short way:
A(cellfun(#(x) strcmp(x,'A2'), A)) = [];
I've created an anynomous function which compares the content of each cell to the string "A2"; applying this to the whole cell array gives me a mask of the cells to delete.
I would suggest you use ismember's second output for this:
[~, ind] = ismember('A2',A)
A(ind) =[]
Or in 1 line using strcmp:
A = A(~strcmp(A,'A2'))
Related
This question already has an answer here:
Storing multiple 2d matricies in one cell array
(1 answer)
Closed 1 year ago.
Hi i am wanting to set a for loop that will create a cell array with each element of the cell array is a matrix, however the elements that i want to store in this is x1,x2,x3,x4… is there a way that I could code this such that the ith element of the array is equal to the the ith variable, ie first element of the cell array would be x1, then second would be x2 etc
Assuming that you have arrays x1 through xi, which you want to store in cell array 'X', you could use the following for loop:
for idx = 1:i
X{idx} = eval(['x' num2str(idx)]);
end
Here ['x' num2str(idx)] forms a string containing x followed by a number and eval evaluates the string as if it was a command.
This question already has answers here:
How to extract numbers from cell in matlab?
(1 answer)
Difference between accessing cell elements using curly braces and parentheses
(2 answers)
Closed 5 years ago.
Let's say: Cell A with size of 1x10 cell:
A={1x10} cell
And the size each matrix in the cell A is like this:
{A}={ [A1] [A2] [A3] [A4] [A5] [A6] [A7] ............ [A10] }
{A}={ [5000x3 double] [3000x3 double] ......... [2222x3 double] }
How can extract each matrix (A1,A2,A3....,A10) from a cell A?
THANKS,
As described in detail in matlab documentation here, you can use curly braces to extract items from a cell array, if you wish for the result to be a simple array and not a cell.
Thus A{2} would give you matrix A2.
This question already has answers here:
How to remove zero entries inside a cell array in MATLAB?
(2 answers)
Closed 5 years ago.
I have an array of cells B. I want to find if one of the cell contains a certain value, in this case [1 1 1440 1920], and if so remove it.
I tried using:
ismember(mat2cell([1 1 1440 1920],1),B)
I got an error saying "Input A of class cell and input B of class cell must be cell arrays of character vectors".
I thought that mat2cell() would give me a cell array. What am I doing wrong?
Is there an easier way to find this component if exist and remove it?
Here's how you can do it using cellfun:
B(cellfun(#(c) isequal(c, [1 1 1440 1920]), B)) = [];
The anonymous function is applied to each cell of B, returning a logical index that is true anywhere the contents of a cell is equal to [1 1 1440 1920]. This index is then used to remove those cells.
If I have a cell array
CELLS = {'AB','AB','AB','BC','BC','CD','CD','CD','DF','FG'}
How do I find the indices of the locations at which the elements change?
in this example I'm looking for an output like:
CHANGES =
4
6
9
10
For a generic cell array of string call unique(), and find(diff(...)) the position index:
s = {'AB','AB','AB','BC','BC','CD','CD','CD','DF','FG'};
[~,~,p] = unique(s)
find(diff(p)==1)+1
This will do:
CHANGES = find(diff(cell2mat(CELLS)))+1
This question already has an answer here:
Convert cell array of cells into cell array of strings in MATLAB
(1 answer)
Closed 9 years ago.
I have a cell array of cell arrays of strings in matlab. I want to convert this to a simple list of unique strings so that I can access a string by its index in the list. What is the fastest way to do this?
Example -
C = {1x3 cell} {1x2 cell}
>> C{1}
ans = 'What's' 'up' 'man'
>> C{2}
ans = 'What's' 'there'
And I want a list of size 4 such that each index refers to a unique word - 'What's', 'up', 'man', 'there'. Not sure if this list should be cell array or matrix or what for it to be most efficient.
Here is the code to do what you need. You can use it for any size of array.
Cunq = unique(horzcat(C{:}),'stable');
You can call union on the cell arrays.
union(C{1},C{2})
If the order matters, add a third parameter:
union(C{1},C{2},'stable')