필터 지우기
필터 지우기

how to save calculating result ( form two variable) in 2-D matrix ?

조회 수: 1 (최근 30일)
Tai-i
Tai-i 2012년 5월 8일
let's me explaining my question :
for example in 1-D
using for loop:
z=[ ];
for Eg=.5:.05:1.3;
a = quad(@(x) (x.^2)./(exp(x/0.516)-1) , Eg , 100 );
z = [z a]
end
then i use "arrayfun" saving result in 1-D matrix
z=[ ];
Eg=0.5:0.05:1.3;
a = arrayfun(@(x1)quad(@(x)x.^2./(exp(x/0.516)-1),x1,100),Eg)
Now i want to this process with 2 variables change to 2-d matrix that Eg is on row and Ei is on column.
(here is my code)
z=[ ];
for Eg=.5:.05:1.3;
for Ei=0:0.05:Eg-0.05
aa= quad( @(E)(E.^2) ./ (exp(E./0.5172)-1), Ei , Eg);
z = [z a]
end
end
thanks :)

채택된 답변

Sargondjani
Sargondjani 2012년 5월 8일
the problem is that the length of Ei will differ for the values of Eg. To solve this with for loop i preallocate 'aa' with NaN everywhere
%with for loop
Eg=.5:.05:1.3;
aa=zeros(17,26)+NaN;
for ig=1:length(Eg);
Ei=0:0.05:Eg(ig)-0.05;
for ii=1:length(Ei);
aa(ig,ii)= quad( @(E)(E.^2) ./ (exp(E./0.5172)-1), Ei(ii) , Eg(ig));
end
end
clear ig ii
without the for loop: i create a 'meshgrid' and then i cut of every value for Ei: if EI>=EG then i set EI=EG-0.05
%without for loop
Eg=.5:.05:1.3;
Ei=0:0.05:Eg(end)-0.05;
[EI,EG]=meshgrid(Ei,Eg);
EI(EI>=EG)=EG(EI>=EG)-0.05;
bb = arrayfun(@(E1,E2)quad(@(x)x.^2./(exp(x/0.5172)-1),E1,E2),EI,EG);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by