Matlab: finding change points in cell array - matlab

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

Related

How do I find a specific cell within a cell array?

Let's say I have a cell array containing 1x2 cells. eg. deck = {{4,'c'},{6,'s'}...{13,'c'}...{6,'d'}}
How can I find the index of a specific cell? E.g I want to find the index of the cell with the values {13,'c'}.
Thanks!
Try cellfun with isequal:
>> deck = {{4,'c'},{6,'s'},{13,'c'},{6,'d'}};
>> targetCell = {13,'c'};
>> found = cellfun(#(c) isequal(c,targetCell),deck)
found =
0 0 1 0
cellfun let's you check anyway you want (not just isequal). For example, if you want to check based on the string element in each cell:
>> targetLetter = 'c';
>> found = cellfun(#(c) strcmp(c{2},targetLetter),deck)
found =
1 0 1 0
Another method I can suggest is to operate on each column separately. We could use logical operators on each column to search for cards in your cell array that contain a specific number in the first column, followed by a specific suit in the second column. To denote a match, we would check to see where these two outputs intersect. We can do this by combining both outputs with a logical AND when we're done:
deck = {{4,'c'},{6,'s'},{13,'c'},{6,'d'}};
target_card = {13, 'c'};
deck_unroll = vertcat(deck{:});
a1 = cat(1, deck_unroll{:,1}) == target_card{1};
a2 = cat(1, deck_unroll{:,2}) == target_card{2};
found = a1 & a2
found =
0
0
1
0
Because deck is a nested cell array, I unrolled it so that it becomes a 2D cell array where each row denotes one card. This is stored in deck_unroll. Once I do this, I further unroll the cells so that the first column gets placed into a numeric array and we search for a particular number (13 in your example) and the second column gets placed into a string array where we search for a particular character ('c' in your example). This is done with the help of cat to extract each element from a particular column and we construct an array out of these elements.

How to find the position of a value in an array in matlab

Think I have an array like
A = { 1,2,3,4,5,6}
I need to get the position of 4 in this array.
I tried,
p = find(A==4)
Please help.
If you really need a cell array (for example because the cells can contain vectors of different sizes):
A = {1, [1 2 3], 4, [1 2], [3 4]}; %// example cell array
sought = [1 2]; %// sought contents
index = find(cellfun(#(x) isequal(x, sought), A));
you created a cell-array instead of a normal vector.
Try:
A = [1,2,3,4,5,6]
find(A==4)
Cell arrays are great to store variables with different types.You could, for example, create a cell array that contains strings as well as digits.
If you have only digits in your array you should defintely use normal arrays.
These are defined by [ ] instead of { }
As you have defined a cell array you need to convert it to a numeric array for find to work, fortunately this is simple to achieve with a couple of well placed brackets.
A = { 1,2,4,3,5,6};
find([A{:}]==4)
ans =
3
So A{:} writes out the numeric values from your array and the [] contains the output for find to work.
p.s. I re-arranged the numbers in A to show that it was working as '4' is now in position 3 to provide a better test.

How can I create a cell array in matlab combining text and number matrices?

The following line works
array ={'a','b'; 1,2};
but
num = [1,2];
array ={'a','b'; num};
doesn't.
I also tried
array ={'a','b'; mat2cell(num)};
and
array ={'a','b'; num2cell(num)};
but neither of them worked. How can I produce a 2x2 cell matrix containing a, b, 1 and 2?
Use concatenation:
array = [{'a','b'}; num2cell(num)]
Or extend the cell array as follows:
array = {'a','b'};
array(end+1,:) = num2cell(num);

Combine Columns of Cell Array Matlab

I have a 2D cell array with dynamic row sizes and column sizes. One example:
cell3 = [{'abe' 'basdasd' 'ceee'}; {'d' 'eass' 'feeeeeeeeee'}]
Gives: (with dimension 2 by 3)
'abe' 'basdasd' 'ceee'
'd' 'eass' 'feeeeeeeeee'
I want to be able to combine the columns and reproduce a aggregate string cell array where each string is separated by a single white space. Any idea how to do that?
So the output i am looking for is:
'abe basdasd ceee'
'd eass feeeeeeeeee'
The final dimension is 2 by 1.
Is this possible?
Apply strjoin either in a loop or via cellfun. The latter:
>> cellRows = mat2cell(cell3,ones(size(cell3,1),1),size(cell3,2));
>> out = cellfun(#strjoin,cellRows,'uni',0)
out =
'abe basdasd ceee'
'd eass feeeeeeeeee'
Solution without loops or cellfun:
[m n] = size(cell3);
cellCols = mat2cell(cell3,m,ones(1,n)); %// group by columns
cellColsSpace(1:2:2*size(cell3,2)-1) = cellCols; %// make room (columns)...
cellColsSpace(2:2:2*size(cell3,2)-1) = {{' '}}; %// ...to introduce spaces
%// Note that a cell within cell is needed, because of how strcat works.
result = strcat(cellColsSpace{:});

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