Correlation of the positions of a multidimensional array to what they represent during initialization - c

I first want to clarify if my logic in the following is correct:
In a 2 dimensional array, say A[x][y], x represents the number of braces and y the number of elements in each brace. So int A[2][3] is initialized as:
{
{1,2,3}, //from A[0][0] to A[0][2];
{4,5,6} //from A[1]{0] to A[1][2];
};
Second, I want to know what the similar correlations are in a 3 dimensional array, four dimensional, and so on, and how to know which dimension number correlates to what level for any multi-dimensional array.
For example, in A[3][4][2], does the 3 denote the number of 2d tables, or rows/columns in each table? And in A[2][3][4][5], does 2 represent the number of 2d tables, no. of 3d tables, no. of 1d tables, or no. of rows/columns per 1d table? Note:I'm getting my head around multi dimensional arrays for the first time, please explain as simplistically as possible.

Yes what you say it's correct. You can think that recursively.
Start from a 1D array (let's assume that it has 3 elements):
int 1darray[] = {0, 1, 2};
Now producing a 2D array simply says go inside every element of 1darray and put another 1D array, to produce a 2D one, like this:
int 2darray[] = {1darray_0, 1darray_1, 1darray_2};
where the 1darray_0, 1darray_1, 1darray_2 are 1D arrays, just like the 1darray we created in the start. So now this will form a 3x3 2D array.
Now the 3D array can be formed like this:
int 3darray[] = {2darray_0, 2darray_1, 2darray_2};
where the 2darray_0, 2darray_1, 2darray_2 are 2D arrays, just like the 2darray we created above. So now this will form a 3x3x3 3D array.
Your example:
A[3][4][2]
says that A has:
3 rows
4 columns
2 z-columns
In general however, I would advice you to have in mind the picture I have in my 2D dynamic array (C):
which describes in a nutshell what I tried to explain in the start.
As you increase your dimensions, you replace every element of the previous array with an array of the next dimension, while you reach the end.

Related

Matlab: How to combine two vectors in one

I have two vectors
A = [...] %size 1x320
B = [...] %size 1x192
I would like to combine the two vectors in one but the way I want to combine them is the following:
Take the first 5 elements of vector A then add 3 elements from vector B add the next 5 elements from vector A then add the next element from vector B and so on until the both vectors are combined in one. I think the process should be repeated 64 times since 320/5=64 and 192/3=64.
Is there any built-in Matlab function to do that?
I don't think that there is a built-in function that does exactly that, but the following will do what you want:
A=randi(10,1,320);
B=randi(10,1,192);
C=zeros(1,length(A)+length(B));
for i=1:5
C(i:8:end)=A(i:5:end);
end
for i=6:8
C(i:8:end)=B(i-5:3:end);
end
Then the array C is the combined array.
Edit: Another way to do that, without for loops:
A=randi(10,1,320);
B=randi(10,1,192);
A_new=reshape(A,5,[]);
B_new=reshape(B,3,[]);
C=[A_new;B_new];
C=reshape(C,[1,numel(C)]);
In this solution, by specifying the third parameter in reshape(A,5,[]) to be [], we allow it to adjust the number of columns according to the length of A, given that the number of rows in the reshaped array is 5. In addition, numel(C) is the total number of elements in the array C. So this solution can be easily generalized to higher number of arrays as well.

How to subtract matrices from elements of cell arrays in a loop?

I have created a cell array of dimensions 1x10, named A. Each element contains a 100x5 matrix. Hence, I got 10 matrices 100x5. However, I want to put every matrix of the cell array into a loop. If B is a 100x5 matrix, C is a 100x1 vector and c is a constant, the loop should look like:
for t=1:100;
j=1:5;
x=c*inv((B(t,j)-A(t,j))*((B(t,j)-A(t,j))')*(A(t,j)-C(t,1)*ones(1,5));
end;
end;
At the end x should deliver a 1x10 cell array that will contain 10 elements of matrices 100x5.
I would appreciate any help. Thank you in advance!
If I understand your question correctly, you are asking how to access a cell array. Let i index the cell array. Then you can access the ith entry of the cell array by calling A{i}. Then your code is:
for i=1:10
for t=1:100
j=1:5
x{i}=c*inv((B(t,j)-A{i}(t,j))*((B(t,j)-A{i}(t,j))')*(A{i}(t,j)-C(t,1)*ones(1,5));
end
end
end
You may want to think about your problem and whether or not you can eliminate the the two middle for loops by writing it in matrix notation. It looks similar to a least-squares estimator, which is (X'X)^(-1)*X'y, but the element-by-element inverse is throwing me off.

C/C++ - What are Tringular Array and Sparse Array?

i really wondering what exactly are Triangular Array and Sparse Array. i have been looking in google but there is no article i found. only discussion and they not talking about the basic. thanks you.
There are wikipedia pages on them.
Sparse array and Triangular array
A sparse array is one in which most of the cells of the array are initialized to null or 0. In such cases we waste a lot of memory. That is why, we generally replace a sparse array with a linked list and when a new cell gets initialized to a non-null value, we just create a node and add the corresponding links properly to reconstruct to linked list. A sparse array is one in which more than 70 percent of the array has null(0) values. When we know that an array is going to be sparse, we generally replace it with a more memory efficient linked list.
A Triangular array is a special kind of a 2-D array where the ith row contains i number of elements. The most simple example is a Pascal's triangle.
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
[nCr = (n-1)C(r-1) + (n-1)C(r)].
So, a[i][j] = a[i-1][j-1] + a[i-1][j]. So a Triangular array helps in the construction of the Pascal's Triangle.
Hope this helps!!

How to convert a cell array of 2D matrices into a multidimensional array in MATLAB

In MATLAB, I have a defined cell array C of
size(C) = 1 by 150
Each matrix T of this cell C is of size
size(C{i}) = 8 by 16
I am wondering if there is a way to define a new multidimension (3D) matrix M that is of size 8 by 16 by 150
That is when I write the command size(M) I get 8 by 16 by 150
Thank you! Looking forward for your answers
If I'm understanding your problem correctly, you have a cell array of 150 cells, and each cell element is 8 x 16, and you wish to stack all of these matrices together in the third dimension so you have a 3D matrix of size 8 x 16 x 150.
It's a simple as:
M = cat(3, C{:});
This syntax may look strange, but it's very valid. The command cat performs concatenation of matrices where the first parameter is the dimension you want to concatenate to... so in your case, that's the third dimension, and the parameters after are the matrices you want to concatenate to make the final matrix.
Doing C{:} creates what is known as a comma-separated list. This is equivalent to typing out the following syntax in MATLAB:
C{1}, C{2}, C{3}, ..., C{150}
Therefore, by doing cat(3, C{:});, what you're really doing is:
cat(3, C{1}, C{2}, C{3}, ..., C{150});
As such, you're taking all of the 150 cells and concatenating them all together in the third dimension. However, instead of having to type out 150 individual cell entries, that is encapsulated by creating a comma-separated list via C{:}.

which structure I should use for this matlab arrays

I'm starting to develop a project which uses multi-dimensional arrays very often.
my arrays are mostly 2 , 3 dimensional or so.
As a 2D array sample consider 'A', I may have 2 or more 1D arrays in a cell.
sth like
A=[1, [78,9] [10,65], 9;
2 , 3 , 6;
7 , [9,1] , [91,41,96][10,-1]]
As you saw in 'A(1,2)' there are two 1D arrays.
I don't know which structure I should use to achieve such thing.
moreover I want to be able to have access to all those 1D arrays.
please share your knowledge with me.
try using cell or struct i would recommend cell.
E.g. preinitialize A1:
A1=cell(3,3)
(that would be a 3x3 cell array/matrix). Then you can adress elements with curly brackets ({}). E.g.:
A1{1,1}= 1;
A1(1,1)={1};
both work. You can also define many cells in one line. E.g:
A1(2,:) = {2,3,6};
For the cases with multiarray entries use another cell structure:
B= {[78,9], [10,65]};
A1(1,2) = {B};
and so on. Pay attention to use curly brackets around B (or the coordinate in A1)! Otherwise he would try to merge the cells from B in A1 but that won't do any good because B is a 1x2 cell and you want to give it as argument to one cell in A1.
If you want to return the value inside a cell you have to use curly brackets again:
A1{1,1}
would return 1.
It depends on what you want to do with such a structure. You could use cell arrays for each 1d array entry, and create a matrix of such cell arrays:
a = {1, 2};
b = {-1, 4, 6};
M = [a b];
Alternatively, you can define a sparse 3d array.

Resources