Store a values in to a table at each iteration of loop

6 views (last 30 days)
Create a table to store the computed values with each itreation of a loop.
With reference to the given code, I want to store the labeled values of f1,f2,f3 in to a table with each iteration of loop
clear all
close all
v1=[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1];
Fs=12000; %sampling frequency
T=1/Fs; %sampling period
L=length(v1); %Lenght of the signal
t=((0:L-1)*T)';
requiredLength=5;
requiredArray3 = [];
frequencyArray = [];
freq = ((1:5)*Fs/5);
%loop
for n = v1
requiredArray3 = [requiredArray3, n];
if numel(requiredArray3) == requiredLength
fastFourier = fft(requiredArray3);
A = fastFourier(:,1:3);
B = freq(:,1:3);
f1 = sum(B.*A)/sum(A);
f2 = sum(requiredLength - f1)/requiredLength;
f3 = sum(sqrt(B-f1).*A)/requiredLength.*sqrt(f2);
requiredArray3 = [];
end
end

Answers (1)

VBBV
VBBV on 20 Oct 2022
clear all
close all
v1=[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1];
Fs=12000; %sampling frequency
T=1/Fs; %sampling period
L=length(v1); %Lenght of the signal
t=((0:L-1)*T)';
requiredLength=5;
requiredArray3 = [];
frequencyArray = [];
freq = ((1:5)*Fs/5);
%loop
for n = 1:length(v1)
requiredArray3 = [requiredArray3, n];
if numel(requiredArray3) == requiredLength
fastFourier = fft(requiredArray3);
A = fastFourier(:,1:3);
B = freq(:,1:3);
f1(n) = sum(B.*A)/sum(A);
f2(n) = sum(requiredLength - f1(n))/requiredLength;
f3(n) = sum(sqrt(B-f1(n)).*A)/requiredLength.*sqrt(f2(n));
requiredArray3 = [];
end
end
f1
f1 =
1.0e+03 * 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 1.3136 + 1.6778i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 1.9348 + 0.4039i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 2.1158 + 0.2228i
f2
f2 =
1.0e+02 * 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i -2.6172 - 3.3556i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i -3.8596 - 0.8078i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i -4.2216 - 0.4455i
f3
f3 =
1.0e+03 * 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.5912 - 1.0897i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i -0.0879 - 2.5229i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i -0.4598 - 3.6071i
  3 Comments
VBBV
VBBV on 20 Oct 2022
clear all
close all
v1=[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1];
Fs=12000; %sampling frequency
T=1/Fs; %sampling period
L=length(v1); %Lenght of the signal
t=((0:L-1)*T)';
requiredLength=5;
requiredArray3 = [];
frequencyArray = [];
freq = ((1:5)*Fs/5);
%loop
for n = 1:length(v1)
requiredArray3 = [requiredArray3, n];
if numel(requiredArray3) == requiredLength
fastFourier = fft(requiredArray3);
A = fastFourier(:,1:3);
B = freq(:,1:3);
f1(n) = sum(B.*A)/sum(A);
f2(n) = sum(requiredLength - f1(n))/requiredLength;
f3(n) = sum(sqrt(B-f1(n)).*A)/requiredLength.*sqrt(f2(n));
requiredArray3 = [];
end
end
T = table(abs(f1).',abs(f2).',abs(f3).','VariableNames',{'f1','f2','f3'})
T = 15×3 table
f1 f2 f3 ______ ______ ______ 0 0 0 0 0 0 0 0 0 0 0 0 2130.9 425.56 1239.8 0 0 0 0 0 0 0 0 0 0 0 0 1976.5 394.32 2524.4 0 0 0 0 0 0 0 0 0 0 0 0 2127.5 424.5 3636.3
VBBV
VBBV on 20 Oct 2022
If you want to store every 5 element, then use
f1 = f1(1:4:end)

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!