How to sum a cell in Matlab - matlab

I am a new learner in Matlab and now want to add up a cell of column elements in Matlab, somehow "sum" function didn't work and it shows "Undefined function 'sum' for input arguments of type 'cell'", is there anyone know how to do it? MANY THANKS!:)
my data is like this:
'218148'
'106856'
'255673'
'156279'
'175589'
'310762'
'87128'
'123339'
'149070'
'104556'
'206346'
'216278'
'235786'

Your cells are strings so you first have to convert them to numeric:
C = { '218148' '106856' '255673' '156279' '175589' '310762' '87128'...
'123339' '149070' '104556' '206346' '216278' '235786' '236087'...
'99137' '123335' '130021' '101655' '98159' '102047' '824411' '63290'};
Csum = sum(str2double(C));
the result:
Csum =
4123952

You can call the content of your cells like this:
your_cell{:}
If all values are numeric, you can then group this result as a vector:
[your_cell{:}]
you can then easily sum this result:
sum([your_cell{:}])
A small example:
c{1} = 1;
c{2} = 3;
c{3} = 6;
sum([c{:}])
result:
ans =
10

Related

construct a matrix with empty string in octave and later fill it with uneven number of characters in each row

I have a working piece of code in MATLAB
empty_string = "";
bag = repmat(empty_string, 4, 1);
bag(2) = "second row";
bag(3) = "third";
However, I want to convert this to octave as MATLAB is a licensed version and everyone can't get access to it.
empty_string = blanks(1);
bag = repmat(empty_string, 4, 1);
bag(2) = "second row";
bag(3) = "third";
This gives error: =: nonconformant arguments (op1 is 1x1, op2 is 1x10)
I want each row of matrix 'bag' to be filled with uneven number of characters, please suggest how this can be done in octave. Thanks.
Since MATLAB strings have not been implemented in Octave, you will need to use a cell array of char arrays. Fortunately, this is pretty simple. Just change the first two lines in your code sample to create a cell array of the proper size:
bag = cell(4,1);
bag(2) = "second row";
bag(3) = "third";
The result is:
bag =
{
[1,1] = [](0x0)
[2,1] = second row
[3,1] = third
[4,1] = [](0x0)
}
The one annoyance is that in order to reference your char arrays in the cell array, you need to use curly braces instead of parentheses:
>> second = bag{2}
second = second row

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

Cell elements as comma separated input arguments for varargin function

Imagine a function with a variable number of input arguments, alternately asking for a string and a value.
myfunction('string1',value1,'string2',value2,...)
e.g.
myfunction('A',5,'B',10)
I want to keep the ability to call the function like that and I dont want to change the evaluation of varargin inside the function. (Except ('string1','string2',...,value1,value2,...) if that helps)
But I also have my input strings and values stored in a cell array inputvar <4x1 cell>:
inputvar =
'A' [5] 'B' [10]
Also this cell array has a variable length.
My intention is to call my function somehow as follows:
myfunction( inputvar )
which is obviously not working. Any ideas how I could transform my cell to a valid input syntax?
I already tried to generate a string like
''string1',value1,'string2',value2'
and use eval to use it in the function call. But it didn't worked out. So alternatively is there a way to transfor a string to code?
You should be able to do it like this:
myfunction(inputvar{:})
{:} creates a comma separated list
EDIT:
For example:
function val = myfunction(string1,value1,string2,value2)
if string1 == 'A'
val = value1;
else
val = value2;
end
myfunction('A',5,'B',10)
myfunction('B',5,'B',10)
A = {'A',5,'B',10};
myfunction(A{:})
A = {'B',5,'B',10};
myfunction(A{:})
returns:
ans = 5
ans = 10
ans = 5
ans = 10

Concatenate strings of digits in matlab

Suppose I have a series of strings such as:
a = '101010101010'
b = '010101'
c = '000101010'
is there a way in Matlab to concatenate them and produce the binary number 101010101010010101000101010?
Use the concatenation operator [ ], with horizontal concatenation , (vertical concatenation ; will fail here unless you reshape() into column vectors):
[a,b,c]
However, I suggest storing your variables in a cell array:
s = {'101010101010','010101', '000101010'};
[s{:}]
or
cat(2,s{:})
To concatenate strings, you could say:
out = [a b c];
Alternatively:
out = strcat(a,b,c);
Yet another way:
out = sprintf('%s', a,b,c);
I think that this should work:
res = [a,b,c]
or alternatively call
res = strcat(a,b,c)
or, yet
res = cat(2,a,b,c)

MATLAB concatenate combination variable length string and vector

Many near-solutions are online, but nothing exact...
I am building a data matrix vector-by-vector:
OutputMatrix(NextSubject,:)=[OutputVector]
I need to lead each row with the name of the data being processed in that loop. The name has the form:
12345.dat
So if OutputVector=[1 2 3 4] the output should look like:
12345.dat 1 2 3 4
I have tried dozens of solutions, but a few examples:
{char(Filename(i).name) OutputVector}
{strcat((Filename(i).name) OutputVector)}
[Filname(i).name OutputVector]
Any help? Please :)
You can't store a string and a vector in a matrix. However, you can do that in a cell.
So you might consider doing:
OutputCell(NextSubject,:) = { Filename(i).name OutputVector };
The curly braces denote that you are storing the object as a cell.
Often though it is better to store strings and number separately. Something like:
OutputMatrix = [];
OutputFile = {};
...
OutputMatrix(NextSubject,:) = OutputVector;
OutputFile{NextSubject} = Filename(i).name;
Then if you access or select rows from output matrix, use the same index for the cell array:
foo(OutputMatrix(index,:), OutputFile(index))