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

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.

Related

Assigning values in an array into another array in MATLAB

I want to assign part of a matrix into another matrix using a for loop in MATLAB. I've tried different ways but none of them worked. I want to know what's wrong with this one:
fullGrid = complex(zeros(FFTLen, numSym, numTx),zeros(FFTLen, numSym, numTx));
for i=0:(numSym/2)-1
for j=0:(FFTLen/2)-1
A(i,j)=[fullGrid(i,j)];
end
end
You made a very basic mistake. The index position in a matrix/array in
Matlab starts from 1 and not 0. So replace all the for loops from 1 to
required length.
Corrected code is given below.
fullGrid = complex(zeros(FFTLen, numSym, numTx),zeros(FFTLen, numSym, numTx));
for i=1:(numSym/2)-1
for j=1:(FFTLen/2)-1
A(i,j)=[fullGrid(i,j)];
end
end

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.

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

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.

Inserting zeros into an array and looping using a for loop

I have several arrays that are calculated example a,b and c (there are more than three) are calculated: Please note this is just an example the numbers are much larger and are not so basic
a=[1,2,3,4,5] b=[10,20,30,40,50] c=[100,200,300,400,500] and I want a for loop that inserts zeros into it so I can have the new_abc array steps look like.
1st for loop step new_abc=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
2nd for loop step new_abc=[1,0,0,2,0,0,3,0,0,4,0,0,5,0,0]
3rd for loop step new_abc=[1,10,0,2,20,0,3,30,0,4,40,0,5,50,0]
4th for loop step new_abc=[1,10,100,2,20,200,3,30,300,4,40,400,5,50,500]
how can I do this with a for loop?
I started with the code below which gives me the zeros
a=[1,2,3,4,5]
new_abc=zeros(1,length(a)*(3));
But I'm not sure how to place the values of the array a b and c using a for loopinto the correct locations ofnew_abc
I know I could place all the arrays into one large array and do a reshape but the calculated arrays I use become to large and I run out of ram, so reading / calculating each array and inserting them into one common array new_abcusing a for loop works best.
I'm running octave 3.8.1 which is like matlab.
This should do it. You can put a,b,c into a cell array. (you can also put them in a matrix...)
new_abc = zeros(1, 3*numel(a));
in = {a, b, c};
for k = 1:3
new_abc(k:3:end) = in{k};
end

How to divide cell array into array and vector

This is my first time posting so i hope you can help me. I am trying to write a function in matlab.
I have laded data from a file into a cell array. First column contains statements and the second contains T for true og F for false. I now want to split this array into a cell array with the statements and a logical vector with 1 for True and -1 for false.
I use the fgetl within a loop to read all the lines into the cellarray
Try to write it a bit more neatly next time, and consider including a small example.
Here is what you seem to be looking for:
Suppose you have a matrix M and want to split that into M_true and M_false
M = {1,'T';
22,'F';
333,'T'}
idx_T=strcmp(M(:,2),'T')
M_true = M(idx_T,1)
M_false = M(~idx_T,1)

Resources