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

조회 수: 1 (최근 30일)
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.

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

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by