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)};
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 would like to know what the meaning of
col2=b1(1:end,lead);
is in MATLAB?
This is very basic stuff. We are led to assume that b1 is a 2-dimensional array. The contents of an array are indexed using brackets, so b1(1,1) will return the top left element of array b1. The first index in your example, 1:end is selecting every element in the first dimension (matlab indexes rows first, then columns). The second index, lead in your example, selects a particular column. We assume that lead has been allocated previously somewhere in the code.
Thence, b1(1:end,lead) returns to col2 a 1-dimensional array containing a subarray of b1. This could be accomplished more elegantly using col2=b1(:,lead);.
I have a multidimensional array A with 1000 elements (1000x3). I have another vector with index positions of elements I want to remove from this array.
I've tried using this A(indices) = [] or A(indices,:,:) = [], but the problem is that the result changes A's dimension, so if indices has 10 elements, I find A's size become 2990x1 instead of 990x3. Anyone can advise how to remove the elements having the indices in A where A's dimensions won't change will still be n x 3?
You can use logical indexing to filter the matrix, for example,
A=rand(1000,3);
A(A(:,1)>0.9)=[];
which removes the rows of A that have a value greater than 0.9 in the first column.
I'm not sure why your original approach didn't work though.
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);
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);