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

조회 수: 4 (최근 30일)
Rajeev Kumar
Rajeev Kumar 2022년 10월 20일
댓글: VBBV 2022년 10월 20일
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

답변 (1개)

VBBV
VBBV 2022년 10월 20일
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
VBBV
VBBV 2022년 10월 20일
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 2022년 10월 20일
If you want to store every 5 element, then use
f1 = f1(1:4:end)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by