Lets say I have following cell array data in Matlab:
>> data = {'first', 1; 'second', 2; 'third', 3}
data =
'first' [1]
'second' [2]
'third' [3]
Then I want to create a new cell array which has only the first column data. I tried the following but got only the first value instead.
>> column_1 = data{:,1}
column_1 =
first
But what I would like to get as output is:
>> column_1 = {'first';'second';'third'}
column_1 =
'first'
'second'
'third'
How can I create a sub-cell from first column of data cell array?
You have to use round parentheses indexing instead of curly braces indexing, like this:
data(:,1)
Output:
ans =
3×1 cell array
'first'
'second'
'third'
Basically, the purpose of curly braces is to retrieve the underlying content of cells and present a different behavior. For extracting subsets of cells you need to use round parentheses. For more details, refer to this page of the official Matlab documentation.
Related
Suppose I use containers map to create a dictionary in MATLAB which has the following map:
1-A;
2-B;
3-C;
Denote the dictionary as D.
Now I have an input list [2,1,3], and what I am expecting is [B,A,C]. The problem is, I can't just use [2,1,3] as the input list for D, but only input 2,1 and 3 one by one for D and get B, A, C each time.
This can get the job done but as you can see, it's a bit less efficient.
So my question is: is there anything else I can do to let the dictionary return the whole list at the same time?
As far as I can find there is no one-step solution like python's dict.items. You can, however, get in a few lines. mydict.keys() gives you the keys of the dict as a cell array, and mydict.values() gives you the values as a cell array, so you can (in theory) combine those:
>> mykeys = mydict.keys();
>> myvals = mydict.values();
>> mypairs = [mykeys',myvals']
mypairs =
3×2 cell array
'A' [1]
'B' [2]
'C' [3]
However, in principle maps are unordered, and I can't find anything in the MATLAB documentation that says that the order returns by keys and the order returned by values is necessarily consistent (unlike Python). So if you want to be extra safe, you can call values with a cell array of the keys you want, which in this case would be all the keys:
>> mykeys = mydict.keys();
>> myvals = mydict.values(mykeys);
>> mypairs = [mykeys',myvals']
mypairs =
3×2 cell array
'A' [1]
'B' [2]
'C' [3]
What is the most efficient way to generate a table from 2 cell arrays. Array A contains data and Array B contains the corresponding names. I would like to combine them into a convenient table structure.
Cell_Array_A =
{[10x10],[10x10],[];
[10x11],[],[10x12];
[9x10],[13x10],[]}
Cell_Array_B =
{['A','B',[];
'B',[],'A';
'B','A',[]}
It should generate a table with the headers 'A' and 'B'. However, in my real data set, I have a lot of variable names, which I don't know. So I need a way to read all variables from the first row and use them to create the table for the rest of the matrix.
Example of the desired output:
'A' 'B'
[10x10] [10x10]
[10x12] [10x11]
[13x10] [9x10]
I tried so far to process the arrays row wise to get rid of the empty arrays. However this is not very efficient. I run the following code for each row in Array A and B. Below an example for the first row in Array A which I later on use as the tablet for my table.
order_row = {};
order_row = Cell_Array_A(1,:);
ordered_filenames = order_row(~cellfun('isempty',order_row));
Edited answer The idea is 1) to collect the non-empty column headers, 2) for each unique column header collect the corresponding values:
% Unique variable names
nul_names = cellfun(#isempty,Cell_Array_B);
var_names = unique(Cell_Array_B(~nul_names));
% Function to find a column header index
ixhead = #(h,c) cellfun(#(x) isequaln(x,h), c(:));
% Collect the values
var_values = cellfun( ...
#(h) Cell_Array_A(ixhead(h, Cell_Array_B)), var_names, ...
'UniformOutput', false ...
);
% Create the table
tbl = table(var_values{:}, 'VariableNames', var_names);
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
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'
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.