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

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.

Related

How to quickly/easily merge and average data in matrix in MATLAB?

I have got a matrix of AirFuelRatio values at certain engine speeds and throttlepositions. (eg. the AFR is 14 at 2500rpm and 60% throttle)
The matrix is now 25x10, and the engine speed ranges from 1200-6000rpm with interval 200rpm, the throttle range from 0.1-1 with interval 0.1.
Say i have measured new values, eg. an AFR of 13.5 at 2138rpm and 74,3% throttle, how do i merge that in the matrix? The matrix closest values are 2000 or 2200rpm and 70 or 80% throttle. Also i don't want new data to replace the older data. How can i make the matrix take this value in and adjust its values to take the new value in account?
Simplified i have the following x-axis values(top row) and 1x4 matrix(below):
2 4 6 8
14 16 18 20
I just measured an AFR value of 15.5 at 3 rpm. If you interpolate the AFR matrix you would've gotten a 15, so this value is out of the ordinary.
I want the matrix to take this data and adjust the other variables to it, ie. average everything so that the more data i put in the more reliable and accurate the matrix becomes. So in the simplified case the matrix would become something like:
2 4 6 8
14.3 16.3 18.2 20.1
So it averages between old and new data. I've read the documentation about concatenation but i believe my problem can't be solved with that function.
EDIT: To clarify my question, the following visual clarification.
The 'matrix' keeps the same size of 5 points whil a new data point is added. It takes the new data in account and adjusts the matrix accordingly. This is what i'm trying to achieve. The more scatterd data i get, the more accurate the matrix becomes. (and yes the green dot in this case would be an outlier, but it explains my case)
Cheers
This is not a matter of simple merge/average. I don't think there's a quick method to do this unless you have simplifying assumptions. What you want is a statistical inference of the underlying trend. I suggest using Gaussian process regression to solve this problem. There's a great MATLAB toolbox by Rasmussen and Williams called GPML. http://www.gaussianprocess.org/gpml/
This sounds more like a data fitting task to me. What you are suggesting is that you have a set of measurements for which you wish to get the best linear fit. Instead of producing a table of data, what you need is a table of values, and then find the best fit to those values. So, for example, I could create a matrix, A, which has all of the recorded values. Let's start with:
A=[2,14;3,15.5;4,16;6,18;8,20];
I now need a matrix of points for the inputs to my fitting curve (which, in this instance, lets assume it is linear, so is the set of values 1 and x)
B=[ones(size(A,1),1), A(:,1)];
We can find the linear fit parameters (where it cuts the y-axis and the gradient) using:
B\A(:,2)
Or, if you want the points that the line goes through for the values of x:
B*(B\A(:,2))
This results in the points:
2,14.1897 3,15.1552 4,16.1207 6,18.0517 8,19.9828
which represents the best fit line through these points.
You can manually extend this to polynomial fitting if you want, or you can use the Matlab function polyfit. To manually extend the process you should use a revised B matrix. You can also produce only a specified set of points in the last line. The complete code would then be:
% Original measurements - could be read in from a file,
% but for this example we will set it to a matrix
% Note that not all tabulated values need to be present
A=[2,14; 3,15.5; 4,16; 5,17; 8,20];
% Now create the polynomial values of x corresponding to
% the data points. Choosing a second order polynomial...
B=[ones(size(A,1),1), A(:,1), A(:,1).^2];
% Find the polynomial coefficients for the best fit curve
coeffs=B\A(:,2);
% Now generate a table of values at specific points
% First define the x-values
tabinds = 2:2:8;
% Then generate the polynomial values of x
tabpolys=[ones(length(tabinds),1), tabinds', (tabinds').^2];
% Finally, multiply by the coefficients found
curve_table = [tabinds', tabpolys*coeffs];
% and display the results
disp(curve_table);

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{:})

MATLAB: plotting multiple columns of a matrix

Inside a MATLAB function I have built a matrix A, whose dimensions M and N are set as parameters of the function. I would like to plot all the columns of this matrix, given a vector of indices B with length M. Hence, I use these lines:
figure
plot(B,A)
I specified figure as the MATLAB function returns more different plots.
My problem is that the program plots just two columns of the matrix with different colours (blue and violet). Where is my mistake?
Thank you for your attention.
go for
plot(repmat(B,1,N),A);
or
plot(repmat(B,N,1),A);
(depending on your rows/columns). You need to have same size matrices in plot.
Moreover, if B are just consecutive indexes, you may want to consider Plot(A) (or Plot(A')).
I noticed that there was an error which caused the overlap of the different curves, so the way which I used to plot the colums of a matrix is valid. However, the method proposed by Acorbe is a possibility, too.

Invalid index error in Scilab when trying to access array element

I'm not sure why I can't do this in Scilab.
-->foo=zeros(500);
-->foo(300)
!--error 21
Invalid index.
Why do I get the 'Invalid index' error? I thought I had initialized foo as an array with 500 elements, each of which was set to 0?
In Scilab, you have to give both the number of rows as well as the number of columns. So, if you want to create a 500x500 matrix, you need to say zeros(500, 500). If you want a 500x1 vector, you need to say zeros(500, 1).
If you want to create a zeros matrix that has precisely as many rows and columns as another matrix (say A), you need to say zeros(A). This is where the confusion stems from.
In Scilab, zeros(500) would take 500 as a 1x1 matrix and generate a zeros matrix of size 1x1, that is [0]. In MATLAB, zeros(500) would take 500 to be the size of the matrix required, assuming a square matrix.
If zeros in Scilab behaves just like zeros in Matlab the call zeros(500) creates a 500x500 array of 0s. That said, foo(300) would be a valid Matlab expression as Matlab understands what it calls 'linear indexing' on arrays of rank greater than 1.
If zeros in Scilab does bot behave just like zeros in Matlab I can't help.
printf("%d\n",Md(y,u))
!--error 21
Índice inválido.
at line 69 of exec file called by :
como soluciono esto?

Need some help understanding the MATLAB `cat` command in high dimensions

The commands
a = magic(3);
b = pascal(3);
c = cat(4,a,b);
produce a 3-by-3-by-1-by-2 array.
Why is the result 3-3-1-2 when the dimension is 4?
Both a and b are two-dimensional matrices of size 3-by-3. When you concatenate them along a fourth dimension, the intervening third dimension is singleton (i.e. 1). So c(:,:,1,1) will be your matrix a and c(:,:,1,2) will be your matrix b.
Here's a link to some documentation that may help with understanding multidimensional arrays.
EDIT:
Perhaps it will help to think of these four dimensions in terms that us humans can more easily relate to...
Let's assume that the four dimensions in the example represent three dimensions in space (x, y, and z) plus a fourth dimension time. Imagine that I'm sampling the temperature in the air at a number of points in space at one given time. I can sample the air temperature in a grid that comprises all combinations of three x positions, three y positions, and one z position. That will give me a 3-by-3-by-1 grid. Normally, we'd probably just say that the data is in a 3-by-3 grid, ignoring the trailing singleton dimension.
However, let's say that I now take another set of samples at these points at a later time. I therefore get another 3-by-3-by-1 grid at a second time point. If I concatenate these sets of data together along the time dimension I get a 3-by-3-by-1-by-2 matrix. The third dimension is singleton because I only sampled at one z value.
So, in the example c=cat(4,a,b), we are concatenating two matrices along the fourth dimension. The two matrices are 3-by-3, with the third dimension implicitly assumed to be singleton. However, when concatenating along the fourth dimension we end up having to explicitly show that the third dimension is still there by listing its size as 1.