Filling in a Cell of Matrices in MATLAB - matlab

In Matlab I am trying to create a cell of size 16 x1 where each entry of this cell is a matrix. I have the following equation
$$W_g = exp^{\frac{j{2\pi m}{N}(n+\frac{g}{G}))} \,\,\,\,\,\,\, m,n=0,1,.....(N-1)$$
for this work assume $N=4$ and the index $g$ is the index that refers to the cell element i.e g=0:1:15
W=cell(16,1);
for g=1:16
for m=1:3
for n=1:3
W{g,m,n}= exp((2*pi*j*m/4)* n+(g-1)/16));
end
end
end
How can I make this work? I have two problems with this, you see g starts from 0 and MATLAB doesnt accept index of zero and how to actually define the matrices within the cell.
Thanks

So if I understand you have this equation:
And you just want the following code:
W=cell(16,1);
n = 1:3;
m = 1:3;
N = 4;
for g=1:16
W{g}= exp((2*pi*j.*m/4*N).*n+(g-1)/16);
end
%or the 1 line version:
W = cellfun(#(g) exp((2*pi*j.*m/4*N).*n+(g-1)/16),num2cell([1:16]),'UniformOutput',0);
With matlab you can use the Element-wise multiplication symbol .*
For example:
%A matrix multiplication
A = [2,3]
B = [1,3]';
result = A * B %result = 11
%An element wise multiplication
A = [2,3]
B = [1,3];
result = A .* B %result = [2,9]

First of all, i is the complex number in matlab (sqrt(-1)) not j, and you are correct, matlab is indexed in 1, so simply start counting g at 1, until 16.
Next, create a zero matrix, and calculate all indices accordingly. Something like this should work just fine :
clc
clear all
W=cell(16,1);
for g=1:16;
temp = zeros(3,3);
for m=1:3
for n=1:3
temp (m,n) = exp((2*pi*1i*m/4)* n+g/16);
end
end
W{g} = temp;
end
if you are considering doing much larger operations, consider using linspace to create your m and n indices and using matrix operations

Related

Matlab: store array in matrix?

I have many array (n*1 dimension), how can I do something like
matrix = [];
for i = 1:5
for j =1:5
matrix (i,j) = zeros(n,1); % store a given array to a cell of a matrix
end
end
I find Array of Matrices in MATLAB
But this is store matrices into array, not the otherwise.
Ying Xiong's suggestion is what you want if the vectors are of different lengths. But assuming the number of elements is constant (which they seem to be) you may also use a 3-dimensional array, where each (i,j) element contains a vector in the third dimension, like this:
rows = 5; cols = 5; n = 10; %// Dimensions
matrix = zeros(rows, cols, n); %// Initialize matrix
vector = 1:n; %// Just an example
for ii = 1:rows %// Bad practice to use i as a variable name
for jj = 1:cols %// Bad practice to use j as a variable name
matrix(ii,jj,:) = vector; %// Assignment
end
end
Now each index (i,j) contains the vectors you want, for instance:
squeeze(matrix(1,1,:))
ans =
1
2
3
4
5
6
7
8
9
10
Having all values in a single matrix can be a good thing if you want to do similar operations on all elements, as vectorized approaches are usually very fast in MATLAB. You might want to check out permute, reshape and functions like bsxfun.
Note that you might be able to vectorize the loops, but without knowing the specifics, that's impossible to know.
You need to use cell array.
n = 10;
matrix = cell(5,5);
for i = 1:5
for j = 1:5
matrix{i,j} = zeros(n,1);
end
end

splitting a matrix row-wise and and finding a linear regression coeff

A= [1 1
2 2
3 3
. .
. .
. .
N N]
I have an [N,2] matrix and I need to split it row-wise into some number of [N/4,2] submatrices. Then for each submatrix I need to find linear regression where the first column of each submatrix is my x data and the second column is my y data. The output should be a struct with fields a,b,c,d.... and values of linear regression for each submatrix
First I tried splitting the matrix with mat2cell where k = length(N)/4 and mat = mat2cell(A, [k k k k], [1 1]).
Next I tried converting mat into struct with out = cell2struct(mat,fields,1) where fields = {'col1','col2'} and use
new = structfun(#(x)polyfit(x.col1, x.col2,1), out,'UniformOutput', false)
But I get the error:
Inputs to STRUCTFUN must be scalar structures.
Does anyone know how to do it? Many thanks
The most straightforawrd way (and probably the fastest) to do this is with a good old for loop:
A = [1:64;1:64]'; % Demo data
m = 4;
N = size(A,1);
k = N/m; % Assumes that length is evenly divisible by 4
c = zeros(m,2); % Coefficients
for i = 1:m
c(i,:) = polyfit(A((i-1)*k+1:i*k,1),A(i-1)*k+1:i*k,2),1);
end
Or rather than using cell2struct and structfun you can use cellfun:
A = [1:64;1:64]'; % Demo data
m = 4;
N = size(A,1);
k = N/m; % Assumes that length is evenly divisible by 4
c = cellfun(#(x)polyfit(x(:,1),x(:,2),1).',mat2cell(A,k+zeros(1,m),2),'UniformOutput',false)
or alternatively
Ac = mat2cell(A,k+zeros(1,m),[1 1])
c = cellfun(#(x1,x2)polyfit(x1,x2,1).',Ac(:,1),Ac(:,2),'UniformOutput',false)
You can convert the output of cellfun to a matrix with:
c = [c{:}].'
As for why you're getting the error, your variable out is a 4-by-1 struct array (array of structures) rather than a simple (scalar) structure of arrays. The documentation for structfun points out this requirement in the description of the short function: "Apply function to each field of scalar structure." This video from The MathWorks tries to explain the difference.

Matlab d-dimensional multipication table?

I'm new to Matlab and I'm trying to solve a problem that involves creating a d dimensional multiplication table where each edge goes from 1 to n. The problem statement says that inputting d = 0 should return the number 1 and d = 1 should return a column vector with the elements 1 to n.
Ideally, I would just create a matrix of 1 to n along d dimensions and then iterate through for each element setting it equal to the product of the indices, but I don't know how to create the d dimensional matrix.
Can anyone help me with this problem?
You can create the table with repeated use of bsxfun. At each iteration, the vector 1,2,...,n is shifted to a new dimension and multiplied (with singleton expansion) by the previous result.
%// Data
d = 3;
n = 10;
%// Computations
vector = (1:n).'; %// first dimension: column vector
result = 1; %// initialization
for n = 1:d
result = bsxfun(#times, result, vector); %// new dimension
vector = shiftdim(vector,-1); %// shift to the next dimension
end

Two for loops (nested), computing matrices Matlab

my current code is below.
What I have is two sets of data phi and theta both 18x30 and I have a for loop running from 1 to 30 which are the 30 columns of my data. Each of these individual columns will produce a matrix 'B' for me. The matrix 'B' is produced after going through the 18 rows of each column.
The problem I have is that I need to multiply all the resulting 'B' matrices for each of the 18 rows with each other in order to get a final matrix for each of the 30 columns, that is why I have set A(:,:,i) = eye(2) so that my first matrix will multiply by A. But I don't want to store A or B for each loop, instead what I want is that on the first loop of i my matrix B will multiply the identity matrix A. Then that matrix A will multiply the next matrix B...with the result of each multiplication being carried forward to the next matrix B that will be calculated, so the multiplications will get done as the program loops. That's why I have the line:
A(:,:,i) = A.*B;
but it doesn't seem to work. I get an error message saying the dimensions must match.
At the end of the program I want to be able to access each of the 30 matrices using a command like:
A(:,:,3), for example, to get my 2x2 matrix for the 3rd column.
Hope that was clear enough!
theta = dlmread('theta.dat');
phi = dlmread('phi.dat');
ne = 1.7;
no = 1.5;
d = 0.000001;
w = 0.000000555;
for i = 1:30
A(:,:,i) = eye(2);
for j = 1:18
nx =((cos(theta(j,i)).^2)/(no^2) + ((sin(theta(j,i)).^2)/(ne^2))).^(-1/2);
D = (2*pi*(nx-no)*d)/w;
x = ((cos(phi(j,i))).^2).*exp((-1i.*D)/2) + ((sin(phi(j,i))).^2).*exp((1i.*D)/2);
y = 1i*(sin(D/2)).*sin(2*phi(j,i));
z = ((cos(phi(j,i))).^2).*exp((1i.*D/2) + ((sin(phi(j,i))).^2).*exp((-1i.*D)/2));
B = [x y;y z];
A(:,:,i) = A.*B;
end
end
B is a 2x2 matrix. For A.*B to work, A must also be 2x2. But A in your program is three-dimensional.
From your problem description, I think you want
A(:,:,i) = A(:,:,i)*B; % Edited now that I see this happens 18 times on the same i
(Please note, I also replaced element-wise multiply .* with matrix multiply *, because that's what it sounds like you want.)
But I suggest
A = eye(2);
and
A = A*B;
and store it at the end like
results(:,:,i) = A;

How can I index a 3-D matrix with a 2-D mask in MATLAB?

Suppose I have D, an X-by-Y-by-Z data matrix. I also have M, an X-by-Y "masking" matrix. My goal is to set the elements (Xi,Yi,:) in D to NaN when (Xi,Yi) in M is false.
Is there any way to avoid doing this in a loop? I tried using ind2sub, but that fails:
M = logical(round(rand(3,3))); % mask
D = randn(3,3,2); % data
% try getting x,y pairs of elements to be masked
[x,y] = ind2sub(size(M),find(M == 0));
D_masked = D;
D_masked(x,y,:) = NaN; % does not work!
% do it the old-fashioned way
D_masked = D;
for iX = 1:size(M,1)
for iY = 1:size(M,2)
if ~M(iX,iY), D_masked(iX,iY,:) = NaN; end
end
end
I suspect I'm missing something obvious here. (:
You can do this by replicating your logical mask M across the third dimension using REPMAT so that it is the same size as D. Then, index away:
D_masked = D;
D_masked(repmat(~M,[1 1 size(D,3)])) = NaN;
If replicating the mask matrix is undesirable, there is another alternative. You can first find a set of linear indices for where M equals 0, then replicate that set size(D,3) times, then shift each set of indices by a multiple of numel(M) so it indexes a different part of D in the third dimension. I'll illustrate this here using BSXFUN:
D_masked = D;
index = bsxfun(#plus,find(~M),(0:(size(D,3)-1)).*numel(M));
D_masked(index) = NaN;
Reshape is basically for free, you can use it here for an efficient solution. reducing the whole to a 2d problem.
sz=size(D);
D=reshape(D,[],sz(3)); %reshape to 2d
D(isnan(M(:)),:)=nan; %perform the operation on the 2d matrix
D=reshape(D,sz); %reshape back to 3d
My Matlab is a bit rusty but I think logical indexing should work:
D_masked = D;
D_masked[ M ] = NaN;
(which probably can be combined into one statement with a conditional expression on the rhs...)