【问题标题】:How to run a matrix column by column in Matlab?如何在 Matlab 中逐列运行矩阵?
【发布时间】:2017-11-25 18:39:32
【问题描述】:

我正在从嵌套的 for 循环中运行一个矩阵。我的问题是这些值出现错误,因为循环逐行填充矩阵。我想让循环逐列填充矩阵以避免这个问题。

T_i = 85;                 %Initial temperature (K)
T_inf = 20;               %Free stream temperature (K)
h = 50;                   %Convection heat transfer coefficient (W/m^2K)
alp = 0.0000015;             %Thermal diffusivity (m^2/s)
k = 15;                    %Thermal conductivity (W/mK)
del_x = 0.03;             %Incremental distance between center nodes (m)
del_t = 300;                %Incremental time diference (s)

Fo = alp*del_t/(del_x^2)     %Find the Numerical/Discretized Fourier Number
Bi = h*del_x/k             %Find the Numerical/Discretized Biot Number
T__vec = [85;85;85;85]     %Initial temperature vector for 4 node points.
%T_inf_vec = 20+zeros(1:10)


M=5             %No. of rows
N=10             %No. of columns


T_inf_vec = [20,20,20,20,20,20,20,20,20,20]


A=zeros(M,N);
A(1:4)= T__vec;
A = [T_inf_vec;A];



 for i=2:M
     for j=2:N
         T_p1=(2*Fo*A(i+1,j-1))+(2*Bi*Fo*A(i-1,j-1))+(((1-2*Fo)-(2*Bi*Fo))*A(i,j-1))
 T_p11 = Fo*A(i-1,j-1)-2*Fo*A(i,j-1)+A(i,j-1)+Fo*A(i+1,j-1);
          if i==2
          A(i,j)= T_p1
          elseif i<1
          A(i,j)= 20
          else
          A(i,j)= T_p11
          end
      end
  end

【问题讨论】:

标签: matlab for-loop if-statement matrix


【解决方案1】:

通过以下方式更改 for 循环:

 for j=2:M
     for i=2:N
         T_p1=(2*Fo*A(i+1,j-1))+(2*Bi*Fo*A(i-1,j-1))+(((1-2*Fo)-(2*Bi*Fo))*A(i,j-1))
         T_p11 = Fo*A(i-1,j-1)-2*Fo*A(i,j-1)+A(i,j-1)+Fo*A(i+1,j-1);
         if i==2
             A(i,j)= T_p1
         elseif i<1
             A(i,j)= 20
         else
             A(i,j)= T_p11
         end
      end
  end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多