Suppose I have ten cell vectors, c{1},c{2},...,c{10}, if I want to extract the unique elements of each cell vector at the same time (in a vectorized manner), how can I do it? I tried to use unique(c), but it did not work.
If you want to have unique elements of each cell then you have to apply unique on each cell, like unique(C{1}), unique(C{2}) etc. This can be achieved by using cellfun.
uniqueCellArray=cellfun(#unique,yourCellArray,'UniformOutput',false);
If your cell array contains a matrix then you may want to use the option 'rows'. If you don't want to sort the outpur of unique, you may want to use the option 'stable'. You can modify the above statement as follows:
uniqueTestCell=cellfun(#(x) (unique(x,'rows','stable')),testCell,'UniformOutput',false);
Related
Is there a way create an N-by-1 cell array of empty characters?
For example, cellstr(repmat('a',2,1)) produces {'a';'a'}.
But cellstr(repmat('',2,1)) or cellstr(char.empty(32,0)) produces what seems to be cell(1). For example, containers.Map({'1','2'},cellstr(repmat('',2,1)) would give a number of keys and values mismatch error.
You can use repmat on a cell of empty string. For example:
repmat({''},2,1);
I have an array of integers called indices and a cell array of strings called Categories. How do I find the most frequent string out of only the cells in the array indices?
You can use unique to find the mode of a cell array by finding the mode of the third output and use that to index into the first output of unique. To determine the mode of only the elements specified by indices, you'll want to grab just the subset of elements of Categories indicated by indices and pass those to unique.
[values, ~, inds] = unique(Categories(indices));
modeValue = values{mode(inds)};
i've got a cell array full of numbers, with 44 rows and different column length in each row
how could i calculate the number of columns in each row?(the columns which their contents are not empty)
i've used 2 different ways which both of them where wrong
the 1st one:
%a is the cell array
s=length(a)
it gives 44 which is the number of rows
the 2nd one
[row, columms]=size(a)
but it doesn't work either cause the number of columns is different in each row.
at least i mean the number of columns which are not empty
for example i need the number of columns in row one which it is 43(a{1 1:43}) but it gives the number of columns for each elements like a{1,1} which is 384 or a{1,2},a{1,3} and so on
You need to access each member of the cell array separately, you are looking for the size of the data contained in the cell - the cell is the container. Two methods
for loop:
cell_content_lengths=zeros(1,length(a));
for v=1:length(a)
cell_content_lengths(v)=length(a{v});
end
cellfun:
cell_content_lengths=cellfun(#length,a);
Any empty cells will just have length 0. To extend the for-loop to matrices is trivial, and you can extend the cellfun part to cells containing matrix by using something like this, if you are interested:
cell_content_sizes=cell2mat(cellfun(#length,a,'uniformoutput',false));
(Note for the above, each element of a needs to have the same dimension, otherwise it will give errors about concatenating different size matrices)
EDIT
Based on your comment I think I understand what you are looking for:
non_empty_cols = sum(~cellfun(#isempty,a),2);
With thanks to #MZimmerman6 who understood it before me.
So what you're really asking, is "How many non-empty elements are in each row of my cell array?"
filledCells = ~cellfun(#isempty,a);
columns = sum(filledCells,2);
This related question How can I apply a function to every row/column of a matrix in MATLAB? seems to indicate one way to do this is using num2cell, which I kind of want to stay away from.
Here's what I want to do. I've got an index list for a triangle mesh, the indices index the vertex list.
I want to run func(a,b,c) on the first 3 indices, then the next three indices, and so on.
So I could reshape(idxs,3,[]) so now i've got my data into triplets as column vectors. But arrayfun does not do what I want it to do.
Looking for something like a column-map operator.
First, get your func properly vectorized, if necessary, such that the arguments can be lists of equal length:
vec_func = #(a,b,c)(arrayfun(#func,a,b,c))
Then, you can directly access every third element of idxs:
vec_func( idxs(1:3:end), idxs(2:3:end), idxs(3:3:end) )
I have a 45x2 cell in MATLAB, with the first column an arbitrarily sized matrix of doubles.
Some of these matrices are repeated, whilst others aren't. I'm attempting to strip out only the unique matrices (but recording the number of repeates), and keep the second column as is.
I've tried a number of things (tabulate, hist et al) but they all fail because of the cell structure (I think). How would one go about doing this, short of looping through each of them individually?
If you convert your matrices to strings, you can run unique on them:
%# create a sample cell array
mc = {magic(3);magic(4);magic(4);magic(5);magic(3);magic(4)}
%# convert to strings
mcs = cellfun(#(x)(mat2str(x)),mc,'uniformoutput',false);
%# run unique
[uniqueCells,idxOfUnique,idxYouWant] = unique(mcs);