Matlab - create a table from a vector in the first row and another in the first column
1 次查看(过去 30 天)
显示 更早的评论
Hello, I am trying to make a table inside Matlab, but it is not coming out with the correct results.
My routine:
alpha=0.008;
gamma=3.3;
g=9.80665;
delta_1=0.07; % if f<=fp
delta_2=0.09; % if f>fp
Tp=2:16;
fp=1./Tp;
T=2:16;
f=1./T;
nrows = 14;
ncols = 14;
E = ones(nrows,ncols);
for fp = 1:ncols
for f = 1:nrows
if f<=fp
E(f,fp) = (alpha*(g^2)*((2*pi)^(-4))*(f^(-5)))*exp((-5/4)*((f/fp)^(-4)))*gamma*exp((-1/2)*((f-fp)/(delta_1*fp))^2);
elseif f>fp
E(f,fp) = (alpha*(g^2)*((2*pi)^(-4))*(f^(-5)))*exp((-5/4)*((f/fp)^(-4)))*gamma*exp((-1/2)*((f-fp)/(delta_2*fp))^2);
else
E(f,fp) = 0;
end
end
end
E
Attached is a figure that is reality (as it turned out) and another as expectation (as it was meant to be).
I thank you for your help.
0 个评论
采纳的回答
Mathieu NOE
2020-10-6
hello
i noticed some mistakes regarding usage of f and fp : you have initialised as vectors of decimal scalars. Then later on, you use them as loop index , therefore overwritting the f and fp values as integers. I guess this was not the intention.
i could not get the expected result because I don't know how the reference (expected) data where generated. i miss this info.
What is sure, is that the row / columns where we expect to see 0 cannot be obtained with your matlab code. So there must be another error somewhere in the logic and / or in the equations.
if you could send me the math behind what you are trying to do, I could better help you.
FYI, this is how I modified your code in the first place
clc
clear all
%%%%%%%%%%
alpha=0.008;
gamma=3.3;
g=9.80665;
delta_1=0.07; % if f<=fp
delta_2=0.09; % if f>fp
Tp=2:16;
% fp=1./Tp;
fp_vector=1./Tp; %
T=2:16;
% f=1./T;
f_vector=1./T;
nrows = 14;
ncols = 14;
E = ones(nrows,ncols);
% ?? f and fp cannot be at the same time used as vector of decimal scalars and index in a for loop
% see correction below
% for fp = 1:ncols % fp loop
for cj = 1:ncols % fp loop
% for f = 1:nrows % f loop
for ci = 1:nrows % f loop
% extract f and fp value
f = f_vector(ci);
fp = fp_vector(cj);
if f<=fp
E(ci,cj) = (alpha*(g^2)*((2*pi)^(-4))*(f^(-5)))*exp((-5/4)*((f/fp)^(-4)))*gamma*exp((-1/2)*((f-fp)/(delta_1*fp))^2);
elseif f>fp
E(ci,cj) = (alpha*(g^2)*((2*pi)^(-4))*(f^(-5)))*exp((-5/4)*((f/fp)^(-4)))*gamma*exp((-1/2)*((f-fp)/(delta_2*fp))^2);
else
E(ci,cj) = 0
% this condition is never reached as all cases are already covered with the two conditions above
% so what condition is supposed to give E = 0 ?
end
end
end
E
2 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!