Add a new element to the beginning of an existing cell array - matlab

I want to add an element into an existing 20x1 cell array so that the array becomes 21x1. I found a way to do add elements at the end from an answer to a previous question in the group
Q{end+1} = 'E1';
But how to do this at the beginning?

Luckily, concatenation works with cell arrays too:
First, let's create a dummy cell array A to use as example:
A = {1;2;'ABC';#(n)sin(n)}
A =
[ 1]
[ 2]
'ABC'
#(n)sin(n)
Now, let's concatenate it with 'E1' using brackets:
A = ['E1'; A]
A =
'E1'
[ 1]
[ 2]
'ABC'
#(n)sin(n)
The more explicit alternative is vertcat (vertical concatenation):
A = vertcat('E1', A)
A =
'E1'
[ 1]
[ 2]
'ABC'
#(n)sin(n)

Related

Function for comparing two cell arrays

I have a cell array (2000*10) with each cell containing a string such as '25:20:55'.
I want to write a function that accepts 10 inputs (say '25:02:33', '58:69:88', '25:54:96', '48:58:36', '58:54:88' and so on) and looks for a match in each column corresponding to input value for that particular column (i.e. the first input data corresponds to 1st column, 2nd to 2nd column of the stored data and so on.
How can I write a function for the above comparison?
Assuming the cell array contains strings, you can find matches using strcmp:
input = {'25:02:33', '58:69:88', '25:54:96', '48:58:36', '58:54:88'};
match_idx = strcmp(repmat(input, size(cell_data,1),1), cell_data);
If you only want to know if there are matches in the respective columns at all and do not care about the line index of the match, you can do
match = any(match_idx,1);
Use ismember
ismember(your_var, your_cell);
e.g.
c = {'a', 'b', 'c', 'd'};
ismember('b', c)
ans =
1
ismember('q', c)
ans =
0
In your case, something like could work
function arr = myfun(inputvec, inputmat)
for i=1:length(inputvec) %// where inputvec is the cell-vector with the strings you want to search for
arr(i) = ismember(inputvec{i}, inputmat{:,i});
end

Add a new element to the end of an existing cell array

As the title already mentions, how is it possible to add a new cell array 1x1 at the end of an existing cell array, let's call him Q, which is a cell array 1x3256?
If you mean adding a single cell to the end (i.e. so your 1-by-3256 cell array becomes a 1-by-3257 cell array) then:
Q{end+1} = []
and you can replace [] with your value directly
Alternatively:
Q(end+1) = {[]}
Adding to Dan's answer, in case you have a cell that is not a single dimension cell, you might want to add a full row, for example. In that case, access the cell as an array using ().
>> c = { 1, 'a'; 2, 'b'}
c =
[1] 'a'
[2] 'b'
>> c(end+1,:) = {3,'c'}
c =
[1] 'a'
[2] 'b'
[3] 'c'

search in the second column of a cell array by the value of the first column

I'm working in MATLAB and I have the following cell array:
pippo =
'FSize' [ 10]
'MSize' [ 10]
'rho' [ 997]
'u2' [ 86.2262]
'n' [ 100]
'nimp' [ 2]
'impeller1dir' [1x66 char]
'impeller2dir' [1x66 char]
'comparedir' [1x57 char]
I would like to return the content of the cell, in the second column, which corresponds to a given value for the cell in the first column of the first row. I.e., if the input is 'nimp', I want to return 2.
Is there a simple way to do this which doesn't involve looping, or is looping the only way?
Two methods to do this are containers.Map and logical indexing
Logical indexing
firstly we will find the occurance of the input in the first column with strcmp using ind=strcmp(pippo(:,1),'nimp') and then get the contents of the cell in the second column where this is true pippo{ind,2}
which can be combined into one line with
out = pippo{strcmp(pippo(:,1),'nimp'),2}
containers.Map
using containers.Map you can map the keys in the first column to the values in the second column this information is stored as a container, below this is the pippo2 variable
pippo2=containers.Map(pippo(:,1),pippo(:,2))
and then you can call the container with an argument of the key and get the value as output
out=pippo2('nimp')
out =
2

Compare two cell array elements in matlab

I am trying to compare two cell arrays, 1x160 (a) and 80x1(b). My cell arrays consist of cells which have a number of strings inside. I wanna compare each string ans see if they are equal, then if they are equal, insert to new array, or insert 0 otherwise. I can't find any function for that. I tried 'isequal','strfind' and others. All of them give me next error message:
If any of the input arguments are cell arrays, the first must be a
cell array of strings and the second must be a character array.
Here is my code!
function [inter]=Intersect2(a,b)
int=cell(0);
b2=[b;b];
for i=1:length(a)
if a{i,1}==b2{i,1}(1) ( or 'isequal','strfind')
int{i}=a{i};
else
int{i}=0;
end
end
There are many ways to compare character arrays, one of which is strcmp.
We'll use cellfun as well to avoid looping.
a = {'Dude', 'I', 'am', 'a', 'moose'};
b = {'Well', 'I', 'am', 'a', 'mouse'};
index = cellfun(#strcmp, a, b);
This will compare each element of a against the corresponding element in b, returning a logical array index that is 1 when the elements match and 0 when they do not.
Use this to assign matching values:
int = cell(1, length(a));
int(index) = a(index);
int =
[] 'I' 'am' 'a' []
You can extend this concept to find the set intersection if you wish.

Getting every two elements from an array in CoffeeScript

I want to use every pair of entries in an array. Is there an effective way to do this in CoffeeScript without using the length property of the array?
I am currently doing something like the following:
# arr is an array
for i in [0...arr.length]
first = arr[i]
second = arr[++i]
CoffeeScript has for ... by for adjusting the step size of a normal for loop. So iterate over the array in steps of 2 and grab your elements using an index:
a = [ 1, 2, 3, 4 ]
for e, i in a by 2
first = a[i]
second = a[i + 1]
# Do interesting things here
Demo: http://jsfiddle.net/ambiguous/pvXdA/
If you want, you could use a destructured assignment combined with an array slice inside the loop:
a = [ 'a', 'b', 'c', 'd' ]
for e, i in a by 2
[first, second] = a[i .. i + 1]
#...
Demo: http://jsfiddle.net/ambiguous/DaMdV/
You could also skip the ignored variable and use a range loop:
# three dots, not two
for i in [0 ... a.length] by 2
[first, second] = a[i .. i + 1]
#...
Demo: http://jsfiddle.net/ambiguous/U4AC5/
That compiles to a for(i = 0; i < a.length; i += 2) loop like all the rest do so the range doesn't cost you anything.​