How to store outputs of a function in a matrix?

37 ビュー (過去 30 日間)
Federica Poli
Federica Poli 2019 年 10 月 2 日
移動済み: DGM 2024 年 7 月 17 日
I need help in storing outputs of a repeated integration in a matrix and then find the max value for each row i (Gam).
Gam = linspace(2,20,19);
Fvpa = zeros(length(Gam),length(w)); %Matrix where I want to store outputs
for i=1:length(Gam)
w = 0.01:0.01:0.99;
for j=1:length(w)
syms q %new variable that I want to use in integration
assume (-0.99<= q <=0.99);
ff=@(q) gamma((v+1)/2)/(gamma(0.5)*gamma(v/2))*(h/v)^0.5*(1+h/v*(q-x_T*beta_OLS).^2).^[-((v+1)/2)];
fq1=@(q) w(j) .* exp(q) + (1-w(j)) .* exp(i_T);
fq2 =@(q) fq1(q).^(1-Gam(i))/(1-Gam(i));
fq3 =@(q) ff(q).* fq2(q);%final function that I want to integrate over q
fqint = int(fq3, q, -0.99, 0.99);
Fvpa=vpa(fqint); %This must be the outputs of each integration that I want then to store
end
end
display(Fvpa);
  1 件のコメント
Rizwan Khan
Rizwan Khan 2020 年 9 月 6 日
% For storing the values
Fvpa(i,j) = vpa(fqint);
%for finding the maximum value of each row of output Fvpa
max = max(Fvpa ,[], 2); % max will be a column vector in which each element is a maximum value of the corresponding row.

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

採用された回答

Charles Rice
Charles Rice 2019 年 10 月 2 日
編集済み: Charles Rice 2024 年 7 月 16 日
You need to move your assignment of w to above your preallocation of the Fvpa variable, or length(w) is meaningless. In your loop, you are assigning the output of vpa(fqint) to a single variable called Fvpa. See line 14 below for the array assignment. If you need the max value for each row, you can just run max(Fvpa, [], 2) to operate on each row. By default, max() returns a row vector containing the max of each column. See help below.
>> help max
max Maximum elements of an array.
M = max(X) is the largest element in the vector X. If X is a matrix, M
is a row vector containing the maximum element from each column.
...
M = max(X,[],DIM) or [M,I] = max(X,[],DIM) operates along the
dimension DIM.
  1. Gam = linspace(2,20,19);
  2. w = 0.01:0.01:0.99;
  3. Fvpa = zeros(length(Gam),length(w)); %Matrix where I want to store outputs
  4. for i=1:length(Gam)
  5. for j=1:length(w)
  6. syms q %new variable that I want to use in integration
  7. assume (-0.99<= q <=0.99);
  8. ff=@(q) gamma((v+1)/2)/(gamma(0.5)*gamma(v/2))*(h/v)^0.5*(1+h/v*(q-x_T*beta_OLS).^2).^[-((v+1)/2)];
  9. fq1=@(q) w(j) .* exp(q) + (1-w(j)) .* exp(i_T);
  10. fq2 =@(q) fq1(q).^(1-Gam(i))/(1-Gam(i));
  11. fq3 =@(q) ff(q).* fq2(q);%final function that I want to integrate over q
  12. fqint = int(fq3, q, -0.99, 0.99);
  13. Fvpa(i, j) = vpa(fqint); %This must be the outputs of each integration that I want then to store
  14. end
  15. end
  16. display(Fvpa);
  3 件のコメント
Federica Poli
Federica Poli 2019 年 10 月 5 日
移動済み: DGM 2024 年 7 月 17 日
Do you know how to find the "w" value related to each maximum?
Charles Rice
Charles Rice 2019 年 10 月 7 日
移動済み: DGM 2024 年 7 月 17 日
>> help max
M = max(X) is the largest element in the vector X. If X is a matrix, M
is a row vector containing the maximum element from each column. For
N-D arrays, max(X) operates along the first non-singleton dimension.
[M,I] = max(X) also returns the indices into operating dimension
corresponding to the maximum values. If X contains more than one
element with the maximum value, then the index of the first one
is returned.
In your case, Gam is your row variable, and w is your column variable, so:
[Fvpa_max_columns, index_columns] = max(Fvpa);
w_max = w(index_columns)
[Fvpa_max_rows, index_rows] = max(Fvpa, [], 1);
Gam_max = Gam(index_rows)

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by