# Populating an Array using a loop

7 views (last 30 days)
Kane OConnor on 21 May 2021
Answered: Nagasai Bharat on 24 May 2021
Hi there im new to matlab
I am wondering how do you run a calculation with a certain input sd, obtain the outputs Ea and E0, save this value and then run the experiment again with a different x value without having to run the experiment numerous times. I assume you use some sort of for loop.
I want to run with sd values 0, 0.01, 0.02.... 0.1
Here is the code below
f=10; %frequency of cosine (Hz)
fs=100; %sampling frequency (Hz)
Ti = 1/fs; %time between samples (s)
pheta = pi/6; %phase
t = 0:Ti:9.99; %time interval set between 0 and N-1
B = cos(2*pi*f*t + pheta); %create vector y containing N samples of the signal
y = B'; %transpose matrix B
Ns = 1000; %number of samples
a = 2*pi*f*Ti*(0:Ns-1)'; %create an array
X = [cos(a) -sin(a)]; %create a matrix
v = mldivide(X,y); %find the elements vector containing br and bi
sd = 0; %set the standard deviation
%Model the effect of noise with that particular sd value
Ndata1 = y + randn(1000,1)*sd;
Ndata2 = y + randn(1000,1)*sd;
Ndata3 = y + randn(1000,1)*sd;
Ndata4 = y + randn(1000,1)*sd;
Ndata5 = y + randn(1000,1)*sd;
Ndata6 = y + randn(1000,1)*sd;
%calculates the v matrix for each experiment
v1 = mldivide(X,Ndata1);
v2 = mldivide(X,Ndata2);
v3 = mldivide(X,Ndata3);
v4 = mldivide(X,Ndata4);
v5 = mldivide(X,Ndata5);
v6 = mldivide(X,Ndata6);
%Estimates the A value from each of the six experiments data
Aest1 = sqrt(v1(1)*v1(1)+v1(2)*v1(2))
Aest2 = sqrt(v2(1)*v2(1)+v2(2)*v2(2))
Aest3 = sqrt(v3(1)*v3(1)+v3(2)*v3(2))
Aest4 = sqrt(v4(1)*v4(1)+v4(2)*v4(2))
Aest5 = sqrt(v5(1)*v5(1)+v5(2)*v5(2))
Aest6 = sqrt(v6(1)*v6(1)+v6(2)*v6(2))
%Estimates theta for the six expermients
phetatest1 = atan(v1(2)/v1(1))
phetatest2 = atan(v2(2)/v2(1))
phetatest3 = atan(v3(2)/v3(1))
phetatest4 = atan(v4(2)/v4(1))
phetatest5 = atan(v5(2)/v5(1))
phetatest6 = atan(v6(2)/v6(1))
%Estimates the Ea value based on these A values
Ea = sqrt(((Aest1-1)*(Aest1-1)+(Aest2-1)*(Aest2-1)+(Aest3-1)*(Aest3-1)+(Aest4-1)*(Aest4-1)+(Aest5-1)*(Aest5-1)+(Aest6-1)*(Aest6-1))*(1/6));
%Estimates the Ea value based on these A values
E0 = sqrt((phetatest1-pheta)*(phetatest1-pheta)+(phetatest2-pheta)*(phetatest2-pheta)+(phetatest3-pheta)*(phetatest3-pheta)+(phetatest4-pheta)*(phetatest4-pheta)+(phetatest5-pheta)*(phetatest5-pheta)+(phetatest6-pheta)*(phetatest6-pheta)*(1/6));

Nagasai Bharat on 24 May 2021
Hi,
From my understanding you are trying to run the above logic with different values of sd and need to store the Ea, E0 values in an array. The the use of functions and arrayfun would help you achive your task. The following code snippet would help you in giving the first impression of doing it.
s = 0:0.01:0.1;
[Ea, E0] = arrayfun(@myFunction,s);
function [Ea, E0] = myFunction (sd)
% Above code enclosed inside here
%Model the effect of noise with that particular sd value
f=10; %frequency of cosine (Hz)
fs=100; %sampling frequency (Hz)
Ti = 1/fs; %time between samples (s)
pheta = pi/6; %phase
t = 0:Ti:9.99; %time interval set between 0 and N-1
B = cos(2*pi*f*t + pheta); %create vector y containing N samples of the signal
y = B'; %transpose matrix B
Ns = 1000; %number of samples
a = 2*pi*f*Ti*(0:Ns-1)'; %create an array
X = [cos(a) -sin(a)]; %create a matrix
v = mldivide(X,y); %find the elements vector containing br and bi
Ndata1 = y + randn(1000,1)*sd;
Ndata2 = y + randn(1000,1)*sd;
Ndata3 = y + randn(1000,1)*sd;
Ndata4 = y + randn(1000,1)*sd;
Ndata5 = y + randn(1000,1)*sd;
Ndata6 = y + randn(1000,1)*sd;
%calculates the v matrix for each experiment
v1 = mldivide(X,Ndata1);
v2 = mldivide(X,Ndata2);
v3 = mldivide(X,Ndata3);
v4 = mldivide(X,Ndata4);
v5 = mldivide(X,Ndata5);
v6 = mldivide(X,Ndata6);
%Estimates the A value from each of the six experiments data
Aest1 = sqrt(v1(1)*v1(1)+v1(2)*v1(2));
Aest2 = sqrt(v2(1)*v2(1)+v2(2)*v2(2));
Aest3 = sqrt(v3(1)*v3(1)+v3(2)*v3(2));
Aest4 = sqrt(v4(1)*v4(1)+v4(2)*v4(2));
Aest5 = sqrt(v5(1)*v5(1)+v5(2)*v5(2));
Aest6 = sqrt(v6(1)*v6(1)+v6(2)*v6(2));
%Estimates theta for the six expermients
phetatest1 = atan(v1(2)/v1(1));
phetatest2 = atan(v2(2)/v2(1));
phetatest3 = atan(v3(2)/v3(1));
phetatest4 = atan(v4(2)/v4(1));
phetatest5 = atan(v5(2)/v5(1));
phetatest6 = atan(v6(2)/v6(1));
%Estimates the Ea value based on these A values
Ea = sqrt(((Aest1-1)*(Aest1-1)+(Aest2-1)*(Aest2-1)+(Aest3-1)*(Aest3-1)+(Aest4-1)*(Aest4-1)+(Aest5-1)*(Aest5-1)+(Aest6-1)*(Aest6-1))*(1/6));
%Estimates the Ea value based on these A values
E0 = sqrt((phetatest1-pheta)*(phetatest1-pheta)+(phetatest2-pheta)*(phetatest2-pheta)+(phetatest3-pheta)*(phetatest3-pheta)+(phetatest4-pheta)*(phetatest4-pheta)+(phetatest5-pheta)*(phetatest5-pheta)+(phetatest6-pheta)*(phetatest6-pheta)*(1/6));
end
To optimize further, global variables could be used instead of declaring each time during the call.