I am trying to import excel windspeed data and to multiply each table variables with the vectors within the for loop, how to change for table variables into vector form?

2 次查看(过去 30 天)
clc; clear all; close all;
% Input data;
g = 9.81;
fs = 0.000003;
w_data = readtable("LakeTahoe_v2_wind_velocity_test.xlsx","ReadVariableNames",true);
%w_data only shows 11.2, 8.0, and 8.1
fi=0.001;
fb=0.05;
rdd=0.005;
ev=20.0;
huo=10.0;
hbo=10.0;
Dx=200.0;
Dt=10.0;
nx=50.0;
nt=103680.0;
% Initial conditions;
for k=1:nx-1
hu(k)=huo;
hb(k)=hbo;
hun(k)=huo;
hbn(k)=hbo;
end
for k1=1:nx
zb(k1)=0;
uu(k1)=0;
ub(k1)=0;
uun(k1)=0;
ubn(k1)=0;
end
%I thought about adding another for loop here to run the main program below with the three different answers above
for i=1:nt
% Continuity equation for the upper fluid layer;
for j=2:nx-2
qul(j)=uu(j)*(hu(j)+hu(j-1))/2;
qur(j)=uu(j+1)*(hu(j)+hu(j+1))/2;
hun(j)=hu(j)-(Dt/Dx)*(qur(j)-qul(j));
end
% Continuity equation for the lower fluid layer;
for j=2:nx-2
qbl(j)=ub(j)*(hb(j)+hb(j-1))/2;
qbr(j)=ub(j+1)*(hb(j)+hb(j+1))/2;
hbn(j)=hb(j)-(Dt/Dx)*(qbr(j)-qbl(j));
end
% Updating of layer depth values;
for j=2:nx-2
hu(j)=hun(j);
hb(j)=hbn(j);
end
% Force equilibrium equation for the upper layer;
for j=3:nx-2
hum(j)=(hu(j)+hu(j-1))/2;
ts(j)=fs*w.*abs(w)/hum(j); %wind velocity is here
ti(j)=fi*(uu(j)-ub(j))*abs(uu(j)-ub(j))/hum(j);
ed(j)=ev*(uu(j-1)-2*uu(j)+uu(j+1))/Dx^2;
uun(j)=uu(j)-Dt*g*(hu(j)+hb(j)+zb(j)-hu(j-1)-hb(j-1)-zb(j-1))/Dx+Dt*(ed(j)+ts(j)-ti(j));
end
% Force equilibrium equation for the lower layer;
for j=3:nx-2
hbm(j)=(hb(j)+hb(j-1))/2;
ti(j)=fi*(uu(j)-ub(j))*abs(uu(j)-ub(j))/hbm(j);
tb(j)=fb*ub(j)*abs(ub(j))/hbm(j);
ed(j)=ev*(ub(j-1)-2*ub(j)+ub(j+1))/Dx^2;
ubn(j)=ub(j)-Dt*g*((hu(j)+hb(j)+zb(j)-hu(j-1)-hb(j-1)-zb(j-1))/Dx-rdd*(hu(j)-hu(j-1))/Dx)+Dt*(ed(j)+ti(j)-tb(j));
end
% Updating of fluid velocity values;
for j=3:nx-2
uu(j)=uun(j);
ub(j)=ubn(j);
Hbfinal(j)=hbn(j);
Htotal(j)=hun(j)+hbn(j);
end
Eu=0;
El=0;
for j=3:nx-2
Eu=Eu+(hu(j)+hu(j-1))/2*uu(j)^2;
El=El+(hb(j)+hb(j-1))/2*ub(j)^2;
end
Hupper(i)=hu(nx-2);
Hlower(i)=hb(nx-2);
end
  1 个评论
KSSV
KSSV 2023-5-23
The below lines:
for k=1:nx-1
hu(k)=huo;
hb(k)=hbo;
hun(k)=huo;
hbn(k)=hbo;
end
for k1=1:nx
zb(k1)=0;
uu(k1)=0;
ub(k1)=0;
uun(k1)=0;
ubn(k1)=0;
end
can be simply replaced by:
hu=huo*ones(1,nx-1);
hb=hbo*ones(1,nx-1);;
hun=huo*ones(1,nx-1);;
hbn=hbo*ones(1,nx-1);;
zb=zeros(1,nx) ;
uu=zeros(1,nx) ;
ub=zeros(1,nx) ;
uun=zeros(1,nx) ;
ubn=zeros(1,nx) ;

请先登录,再进行评论。

回答(1 个)

Sugandhi
Sugandhi 2023-6-7
Hi,
I understand that you want to convert table variables to vector form.
In MATLAB, you can convert a table variable to a vector using the table2array and reshape functions. Here's an example:
% Create a sample table
T = table(rand(3,1), rand(3,1), rand(3,1), 'VariableNames', {'Var1', 'Var2', 'Var3'})
T = 3×3 table
Var1 Var2 Var3 _______ _______ _______ 0.74342 0.76146 0.16319 0.54526 0.36217 0.86704 0.88032 0.22428 0.99576
% Convert table to a numerical array
A = table2array(T)
A = 3×3
0.7434 0.7615 0.1632 0.5453 0.3622 0.8670 0.8803 0.2243 0.9958
% Reshape array to column vector
v = reshape(A, numel(A), 1)
v = 9×1
0.7434 0.5453 0.8803 0.7615 0.3622 0.2243 0.1632 0.8670 0.9958
In the code above, the `table` function is used to create a sample table with 3 rows and 3 variables. The `table2array` function is used to convert the table to a numerical array. The `reshape` function is then used to convert the array into a column vector.
For more understanding, kindly go through following links:

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by