Simulink: Use array of structs or a cell matrix - simulink

I have an array of polyhedron objects (A matrix A and vector b that describe a region x in N dimensional space such that A*x <= b). I have like 600 or so polyhedrons in total.
I need to use these polyhedrons in my Simulink simulation. The A matricies and b vectors are not the same size, so I cannot create a 3d tensor of A matricies or a 2d matrix of b vectors (unless I added 0s to them to resized them all to the largest one, but I'm wondering if there is a better way).
I read that structure arrays and cell matricies are not allowed in Simulink. My question is if anyone knows any tricks to be able to easily use an array of variable sized matricies in Simulink? Thanks!

A few options:
A structure field can be variable-sized, so a structure with variable-sized fields might be your solution.
Array of structures with extra parameter "n" that is used as a counter, to mimic variable-sized behaviour.
Do all mathematics and storage in a MATLAB Function block - which allows dynamic sizing.

Related

Nonlinear curve fitting of a matrix function in python

I have the following problem. I have a N x N real matrix called Z(x; t), where x and t might be vectors in general. I have N_s observations (x_k, Z_k), k=1,..., N_s and I'd like to find the vector of parameters t that better approximates the data in the least square sense, which means I want t that minimizes
S(t) = \sum_{k=1}^{N_s} \sum_{i=1}^{N} \sum_{j=1}^N (Z_{k, i j} - Z(x_k; t))^2
This is in general a non-linear fitting of a matrix function. I'm only finding examples in which one has to fit scalar functions which are not immediately generalizable to a matrix function (nor a vector function). I tried using the scipy.optimize.leastsq function, the package symfit and lmfit, but still I don't manage to find a solution. Eventually, I'm ending up writing my own code...any help is appreciated!
You can do curve-fitting with multi-dimensional data. As far as I am aware, none of the low-level algorithms explicitly support multidimensional data, but they do minimize a one-dimensional array in the least-squares sense. And the fitting methods do not really care about the "independent variable(s)" x except in that they help you calculate the array to be minimized - perhaps to calculate a model function to match to y data.
That is to say: if you can write a function that would take the parameter values and calculate the matrix to be minimized, just flatten that 2-d (on n-d) array to one dimension. The fit will not mind.

What is the difference between matrix and array?

What is the more generalized term?
Why is MATLAB named matrix laboratory, then?
A matrix is a practical way to represent a linear transformation from a space of dimension n to a space of dimension m in the form of a nxm array of scalar values.
It is also very practical to perform linear algebra operation in a very systematic way that can be implemented on a computer. For instance if matrix A represents the linear transformation f and matrix B the linear transformation g, then the composition f o g writes as A*B where * denotes matrix multiplication. Matlab has also a lot of routines related to matrix operations (i.e. linear algebra operations) like det, pinv, svd etc...
As you can still see nowadays in Matlab, operators like *, / are strongly tied to matrix operations and thus strongly tied to linear algebra operations, which I think was the original goal of matlab in its early elaboration, hence its name (surely quite speculative but guess not so far from reality).
To perform element-wise operations on n-dimensional data sets, you have to write .*, or ./. denoting you are now performing array operations.
I would not say array operations encompass matrix operations, they are different. The later ones relate to linear algebra, while the other ones just relate to a practical way to operate on large sets of data. These data are not limited to be numbers, they are just n-dimensional data sets of whatever (string, numbers, cells, etc...).
Matlab also has a very synthetic syntax to perform array operations on sub-blocks (i.e. linear/logical subscripts) that makes it very easy to reorganize data sets in just one line of code before applying subsequent matrix or array operations.
If you're asking about MATLAB, the word "matrix" typically refers to a 2d array, whereas an "array" can be n-dimensional.
Early versions of MATLAB supported only 2d matrices, not n-dimensional arrays. I believe support for n-dimensional arrays was introduced in version 5 of MATLAB.
I would say that MATLABs matrix is a more advanced kind of array if you compare to the c-style arrays, eg double array[], or the Java array, eg double arry2[]. I would also say that the matlab matrix is better for mathematical purposed than the c++ vector or Java ArrayList. However, if you mean the matlab array I would say that it is more complicated. I would then recommend the link about matlab data which describes the mxArray type, used to store most of the data in matlab. The question is hard to answer completely without better description of what you mean with array, but I would say that regarding the type there is no difference between an array like a = [1,2,3,4] and matrix like b = [1,2,3,4;5,6,7,8]. There can also be matrices of higher dimensions as c = ones(3,4,3). These are in general called matrices as well in MATLAB, or if you need to be more specific N dimensional matrices.

Submatrix based on size vector

It seems like this problem should be common, but I haven't found a good duplicate...
I'm implementing a level 2 S-function with a variable-sized multidimensional output. The state has to be in fixed-size Dwork vectors, so I zero-pad the input matrix to the maximum size allowed for the input and then reshape it to a vector.
When I reshape it back to a matrix for output, I need to trim it back down to the correct size.
The function needs to be general enough to support an arbitrary number of dimensions. The size of the output is stored in a size array.
For example, I may have a 500x500 matrix N, and a size array S = [40 25]. I need a MATLAB expression that would give me N(1:S(1), 1:S(2)), but it needs to work for any number of dimensions so I can't simply hardcode it like that.
Here is a solution in m-code:
%your input
M=rand(10,10,10);
S=[2,3,4]
%generate indices:
Index=arrayfun(#(x)(1:x),S,'uni',0)
%use comma separated list to index:
smallM=M(Index{:})

When the input matrix is supposed to be: "The rows of X correspond to observations, and columns correspond to variables."?

I'm not getting the correct results from the Matlab function so maybe my data arrangement is wrong. I looked at the help file of the function I am using and the input, "X" that it takes must be in the form.
The rows of X correspond to observations, and columns correspond to
variables.
I am sorry if this is very basic but how exactly should my input matrix be arranged?
I have 5 writers, each have a feature vector of length 18 (for example for the sake of simplicity).
So I assumed that by observations it is meant the different features of the same writer and variables mean the writers, so I arranged the input matrix as [18 x 5] where each column is a writer.
This example is simple. What of in the case of SIFT features? where each writer will produce a feature matrix [128 x num. of keypoints] which usually becomes [128 x 70] for one image. So if I want to concatenate all of them into the input matrix my input matrix will become [128 x 350].
Will this just be the input matrix X? Then in the case of SIFT each variable in 70 columns wide.
Thank you in advance
If all of your writers data have different size, I suggest you to use cell() which is cell array. http://www.mathworks.com/help/matlab/cell-arrays.html - here is your reference. So for example if you need to calculate covariance you can do it for each matrix separately. Then your covariance matrices will be same size(128*128) so you can put them together and have your 3D matrix data.
Hope it will help you.

What is a Vector

I am from C/C++ programming world and finding it difficult to understand what exactly is a Vector / Matrix in MATLAB - why are the not termed as array everywhere.
What is Vector in MATLAB and why it is not called or referenced as an array?
The "MAT" in MATLAB is for Matrix, not Math. In MATLAB, basically everything you do is calculations with what you would call matrices / vectors in mathematical terms.
It is common to call a numeric array a matrix (or vector if it's 1xn), and other arrays for arrays. You'll see terms like cell array, which is an array of cells.
This way you can use mathematical terms when describing calculations with numerical arrays. For instance inv can be used to find the inverse of a matrix, instead of the inverse of a numeric array. (Btw, never use inv, it was just an example).
Matlab is designed to use as "Matrix-Lab": a tool for numerically process linear-algebra objects such as vector and matrices. So, in terms of "data structures" it indeed works with n-dimensional arrays, but has special names for the special cases: "vector" for 1-d array and "matrix" for 2-d array.