I was wondering is it possible in C language to save couple of matrices in an array and how to do that? Like, I pass a static matrix to a function and in a several steps I use the same matrix for different calculations, so I need to save every matrix with different result somewhere, so is it possible to save matrix as element of an array?
so is it possible to save matrix as element of an array?
Yes, you can use a three dimensional array to store it's elements as matrices. Something like array[no_of_matrices][row_no][column_no] would do fine
example:
int arr[2][2][2];
// this would store 2 matrices of dimensions 2*2
Additionally, if you want arrays of different dimensions then you can create **array[no_of_matrices] and use dynamic memory allocation to allocate memory according to required dimensions of each matrix.
Related
Hi I have many arrays of different lengths now I want to create ONE long array (1D) out of all of them. Counterintuitively vertcat gives me a dimension error even though I do not see the point why the dimensions of my arrays should match.
Am I using vertcat wrong?
Your vectors are probably column vectors of different lengths (or matrices). Suppose A to D are the matrices you want to create a 1D-vector from. Try "flattening" them out using (:), and vertcat thereafter, like this:
long_1D_vector = [A(:); B(:); C(:); D(:)];
You may transpose it if you want a column vector instead:
long_1D_vector = [A(:); B(:); C(:); D(:)].';
In short:
I need to load multiple 2d numpy matrices - with different sizes - into one numpy matrix. First, I tried to load them as the 3rd dimension of the matrix but due to the various sizes it wasn't possible. It would be great if you could direct me how to do it, or even any other convenient solution.
More description:
I'm reading arrays from multiple csv files and loading them into numpy arrays. This is easy when the number of csv files are known and the arrays are at the same size. Whereas, I need to do this for any number of files at different sizes. So I thought I could use numpy's structured arrays, but I can't do it.
I think Numpy is going to require mathematically consistent matrices... which means all rows/columns in a 2D matrix have to have the same number of elements. Likewise, if you try to make a 3-dimensional matrix, each 2D matrix will have to have the same dimensions.
However, you could use a regular Python array of numpy matrices. If you do that, then you can dynamically add/remove individual matrices, and they can have varying dimensions.
I have a couple of fints, how do I preallocate a cell array so that I can loop through them later? I don't really care if they are stored as a cell array or array or anything different, I just want to be able to do the following
for(i = 1:size(stocks))
figure(i);
plot(stocks(i));
end
or something equivalent. allocating with stocks = zeros(0,5) works great first, but doesn't work when I try to insert the fints because it is assumes it is a double. How would you even go about preallocating arrays for financial time series obejcts? Since it would be different lenghts everytime you insert a new one.
From the Matlab's doc on Preallocate Memory for a Cell Array
Cell arrays do not require completely contiguous memory. However, each
cell requires contiguous memory, as does the cell array header that
MATLABĀ® creates to describe the array. For very large arrays,
incrementally increasing the number of cells or the number of elements
in a cell results in Out of Memory errors.
Initialize a cell array by calling the cell function, or by assigning
to the last element. For example, these statements are equivalent:
C = cell(25,50); C{25,50} = [];
MATLAB creates the header for a
25-by-50 cell array. However, MATLAB does not allocate any memory for
the contents of each cell.
Does anyone know how to do array matrix multiplication in matlab? i.e. I have two 3 dimensional arrays consisting of sets of matrices in the first 2 dimensions and I would like to multiply each matrix in the first array with the corresponding one in the second array. So, i.e. if
A=randn(3,3);
B=cat(3,A,A);
I would like [[operation]] such that
B[[operation]]B = cat(3,A*A, A*A)
done in efficient vector form.
Many thanks in advance.
I have used MULTIPROD from the Mathworks FileExchange for N-D array multiplication before. It is basically an extension of bsxfun to N-D arrays, and works quite nicely (and fast) - although the interface is a bit cumbersome.
I want to have a time series of 2x2 complex matrices,Ot, and I then want to have 1-line commands to multiply an array of complex vectors Vt, by the array Ot where the position in the array is understood as the time instant. I will want Vtprime(i) = Ot(i)*Vt(i). Can anyone suggest a simple way to implement this?
Suppose I have a matrix, M(t), where the elements m(j,k) are functions of t and t is an element of some series (t = 0:0.1:3). Can I create an array of matrices very easily?
I understand how to have an array in Matlab, and even a two dimensional array, where each "i" index holds two complex numbers (j=0,1). That would be a way to have a "time series of complex 2-d vectors". A way to have a time series of complex matrices would be a three dimensional array. (i,j,k) denotes the "ith" matrix and j=0,1 and k=0,1 give the elements of that matrix.
If I go a head and treat matlab like a programming language with no special packages, then I end up having to write the matrix multiplications in terms of loops etc. This then goes towards all the matrix operations. I would prefer to use commands that will make all this very easy if I can.
This could be solved with Matlab array iterations like
vtprime(:) = Ot(:)*Vt(:)
if I understand your problem correctly.
Since Ot and Vt are both changing with time index, I think the best way to do this is in a loop. (If only one of Ot or Vt was changing with time, you could set it up in one big matrix multiplication.)
Here's how I would set it up: Ot is a complex 2x2xI 3D matrix, so that
Ot(:,:,i)
references the matrix at time instant i.
Vt is a complex 2xI matrix, so that
Vt(:,i)
references the vector at time instant i.
To do the multiplication:
for i = 1:I
Vtprime(:,i) = Ot(:,:,i) * Vt(:,i);
end
The resulting Vtprime is a 2xI matrix set up so that Vtprime(:,i) is the output at time instant i.