print values (__repr__) for vector of self-defined data type, using pybind11 - pybind11

The binding of the vector of int/uint/..., print(vectorOfInt) will just print the list of values.
But for a vector of self-defined data type, it prints out the address instead, is there an elegant way to print the list of values for the self-defined type?
Thanks.

Related

In PySpark, what's the exact value of lambda x,y in the processing of `rdd.reduce(lambda x,y:x+y)`?

For example,
rdd=sc.parallelize(range(5),5)
rdd.reduce(lambda x,y:x+y)
Then what's the exact value of lambda x,y in the processing?
I assume that we can just process one value when there is an array as an input. But lambda get two values.
Sorry I can't print it as usual in python.

How to convert EditField value to cell array?

I want to take a cell array from the user containing the number of zeros and poles of each transfer function in a system identification app that I am designing in MATLAB's app designer.
User enters something like this:
{[2,1], [1,0]; [1,0], [2,1]}
EditField or TextArea treats this input as a char array or string, But I want to re-convert it to a cell array of numbers, not strings. How is that possible?
You can use eval to evaluate the string to get the resulting numbers. This works if it has numbers, variables and functions accessible from the workspace where you are running eval. See documentation for eval at https://www.mathworks.com/help/matlab/ref/eval.html. If there is a variable in the expression for example as, {[2,1], [1,0]; [1,0], a} with a defined in the base workspace then you need to use evalin. evalin lets you specify the workspace where the expression needs to be evaluated.
Finally if it is not a cell array and contains only an array of numbers then str2num also can do the job of converting the string to numbers.

Array as a function parameter instead separate variables in Matlab

I need pass to a function (jacobian() in my case) a symbolic variable array is being creating dynamically. Say,
jacobian(handles{2}(t,y,paramlist),y)
where paramlist=[var1, var2, var3, ..., varN] has an arbitry size. All variables here are symbolic and have various names. MATLAB throw an error:
Not enough input arguments.
Knowing number of parameters in the function definition one can pass all parameters separately. Say, for n=3:
jacobian(handles{2}(t,y,paramlist(1),paramlist(2),paramlist(3)),y)
But what about the common case? It's a bad style of programming to write the function call for each fixed number of parameters. Is there a way to pass an array so as it would be treated as distinct variables?
You can convert paramlist to a cell array (using num2cell) and then use {:} indexing to create a comma separated list which you can then use this for indexing into handles{2}. This will make it such that each value of paramlist is passed as a separate subscript.
plistcell = num2cell(paramlist);
jacobian(handles{2}(t, y, plistcell{:}), y)

one example about arrayfun in matlab

I am new to matlab. I have the following code like this:
names=arrayfun(#num2str, 1:nseq_all, 'unif', 0);
After searching, I understand that arrayfun applies a function to each element of an array. So, I guess in this case, we apply num2str function to each element of the array 1:nseq_all. 'unif' and 0 are the arguments for the function and the corresponding value part. I have trouble to understand this part. Any comments are greatly appreciated.
'unif',0 is shorthand for 'UniformOutput',false, which means that the output does not have the same dimensions as the input array 1:nseq_all.
This is because a string 1 dimensions 1x1, but 124 has dimensions 1x3.
names will be a cell array, as a normal numeric array can't contain rows/columns with different numbers of elements.
Be sure to carefully read the arrayfun documentation.
'unif', 0 is shorthand for 'UniformOutput',false. Note that parameterName/parameterValue pairs in Matlab allow the abbreviation of the parameterName, as long as it doesn't conflict with another possible parameter. In this case 'un',0 would have worked as well. Anyway, what is that option for?
modifiedArray = arrayfun(function_handle, array)
applies the function defined in function_handle to each element of array and returns modifiedArray, which is the same size as array, and the class of whatever function_handle returns. This syntax can only be used if the output of function_handle is scalar, though class doesn't matter, so the output of function_handle can be a scalar structure, i.e. arrayfun(#(x)struct('field',x),magic(4)) is valid.
cellArray = arrayfun(function_handle, array, 'UniformOutput', false)
applies the function defined in function_handle to each element of array and returns cellArray, which is the same size as array. Each element of cellArray contains the output of a call of function_handle. This syntax must be used if the output of function_handle is non-scalar (even if each calculation returns a 1-by-2 array, which is totally uniform), but of course, you can use it with scalar output as well.
In your case, num2str returns a character array which is non-scalar if the argument of num2str goes above 9. Consequently, you need to set UniformOutput to false.

MATLAB: Find numeric columns in a cell array

I would like to use gplotmatrix on a dataset data, which contains mixed data (numeric and strings). However, gplotmatrix works on numeric data, so I need to convert my dataset to a matrix. As far as I know, the only way is to do this is through
C=dataset2cell(data)
X=cell2mat(C)
However, the second command throws an error, because C contains non-numeric columns. Is there a way to find which columns of a cell array are purely numeric?
Use cellfun with #isnumeric function handle -
numeric_cols = find(all(cellfun(#isnumeric,C)))
Related useful pointers -
function_handle
Anonymous Functions