How do I get the combination of cell array "wmn" and set up the preallocation of "wmn"

1 回表示 (過去 30 日間)
Mark
Mark 2021 年 6 月 8 日
コメント済み: Mark 2021 年 6 月 8 日
Below is my code.
In the last line of my code. Because "i" is variable, I would like to ask how to combine the wmn without using "Combine=wmn{1}+wmn{2}+wmn{3}+wmn{4}". So I can combine more term when "i" is a large number.
Also, about the second for loop, I would like to know how should I preallocate the "wmn array" with zero matrix, just as I did for the first for loop "amn=zeros(1,len)" so that I can do the calculation more efficiently.
Thank you very much!!
clc
clear
format long
E=209e+3;
q=-1;
h=15;
D=6.459478021978022e+07;
I=2.8125e+02;
a=600;b=2400;
% change the value of mn
n =7;
[T1, T2] = meshgrid(1:2:n);
mn = [T1(:), T2(:)]
syms x y
x_value=0:10:600;
y_value=0:10:2400;
[X,Y]=meshgrid(x_value,y_value)
% Fine the amn
len=length(mn);
amn=zeros(1,len);
for i=1:len
m=mn(i,1);
n=mn(i,2);
amn(i)=(16*q/(m*n*D*pi^6))*(1/((m/a)^2+(n/b)^2)^2); % amn(i) storage the array
end
% Fine the wmn
% How do I preallocate the wmn array?
for i=1:len
m=mn(i,1);
n=mn(i,2);
wmn{i}=amn(i).*((sin(m.*pi.*X./a)).*(sin(n.*pi.*Y./b)));
end
% How do I find the combination of wmn ?
Combine=wmn{1}+wmn{2}+wmn{3}+wmn{4}

採用された回答

KSSV
KSSV 2021 年 6 月 8 日
編集済み: KSSV 2021 年 6 月 8 日
clc; clear all ;
clc
clear
format long
E=209e+3;
q=-1;
h=15;
D=6.459478021978022e+07;
I=2.8125e+02;
a=600;b=2400;
% change the value of mn
n =7;
[T1, T2] = meshgrid(1:2:n);
mn = [T1(:), T2(:)]
syms x y
x_value=0:10:600;
y_value=0:10:2400;
[X,Y]=meshgrid(x_value,y_value) ;
% Fine the amn
len=length(mn);
amn=zeros(1,len);
for i=1:len
m=mn(i,1);
n=mn(i,2);
amn(i)=(16*q/(m*n*D*pi^6))*(1/((m/a)^2+(n/b)^2)^2); % amn(i) storage the array
end
% Fine the wmn
wmn = zeros(size(X,1),size(X,2),len) ; % preallocation
for i=1:len
m=mn(i,1);
n=mn(i,2);
wmn(:,:,i)=amn(i).*((sin(m.*pi.*X./a)).*(sin(n.*pi.*Y./b)));
end
Combine=sum(wmn(:,:,1:4),3) ; % use the function sum
  3 件のコメント
KSSV
KSSV 2021 年 6 月 8 日
Type error:
Replace
Combine=sum(wmn(:,:,1:4),[],3) ;
with
Combine=sum(wmn(:,:,1:4),3) ;
Edited the answer.
Mark
Mark 2021 年 6 月 8 日
It works! Thank you very much!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by