How can extract elements from a cell array [duplicate] - matlab

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.

Related

For loop with set variable but slight change [duplicate]

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.

Converting 5D cell containing 1x5 double elements into matrix

I have a cell which is of dim x{5,5,5,7,4} and each element it contains is a 1x5 double. I want to convert x from cell to matrix or double. Now when I open x it just says the elements are 1x5 double. How to do this?
If all of your elements are 1x5 double arrays, then you can use cat (combined with {:} indexing) to concatenate all contents along the first dimension
result = cat(1, x{:})
I think this should work with a combination of eval or feval and cell2mat, which gives you a matrix out of a cell.
You can only use cell2mat on the 'second-lowest' level of the cell, so in this solution you would have to iterate through 4 the 5 dimensions of your cell to give every second-lowest-level cell x{k}{l}{m}{n} a name like 'M_int2str(k)_int2str(l)_int2str(m)_int2str(n)' with eval. After that, concatenate the resulting 5*5*5*7 matrices in a way you would like it.
Another - probably easier and faster - possibility would be to create a cell-pyramid with cell2mat. A shorter example:
C={{[1,2,3],[4,5,6]},{[7,8,9],[10,11,12]}};
for k=1:2
M{k}=cell2mat(C{k});
end
N=cell2mat(M)
The result of this is
N =
1 2 3 4 5 6 7 8 9 10 11 12.

How do I find if a cell in a cell array contains a certain value in MATLAB? [duplicate]

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.

Find and delete row from cell array [duplicate]

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'))

Convert cell arrays of cell array to list in matlab [duplicate]

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')