Loading variables & using 'for' loop for comman polyeig()

Let us say, i've a series of matrices as K1, K2,....K(n)
I want to obtain eigen values and eigen vectors for the above mentioned matrices; like:
[X1 E1] = polyeig(K1,M)
[X2 E2] = polyeig(K2,M)
[X3 E3] = polyeig(K3,M) & so on
Can anyone suggest a for loop for it, where M is not a variable matrix. But X1,X2,X3...... & E1,E2,E3..... are variables.

 Accepted Answer

It is not a good idea to name variable like K1, K2, K3, ... It makes code complicated: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval. First I will convert K1, K2, ..., Kn to cell array and then return the output in cell arrays.
K = {K1, K2, K3, .., K10}; % list all the Kxx variables here
M; % matrix
[X, E] = cellfun(@(k) polyeig(k, M), K, 'uni', 0);
Access the results like this
X{1}; % first element of X
X{2}; % second element of X
..
X{end}; % last element of X
E{1}; % first element of E
E{2}; % second element of E
..
E{end}; % last element of E

5 Comments

Hi Ameer,
I really want to thank you for giving your valuable time on it and giving this solution. It is showing some error for me, i don't know why. So, I'm sharing mine code, so that you will be in better position to answer the problem.
close all
clc
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
m1 = 16500;
m2 = 16100;
M = [m1 0;0 m2];
x1 = linspace(0,3,5000);
x2 = linspace(0,2,5000);
k1 = 2.968888888888889e+07; %(k1=k2)
Ks1 = k1*x1;
Ks2 = k1*x2;
for i = 1:length(x1)
eval(['K' num2str(i) '= [Ks1(1,i)+Ks2(1,i) -Ks2(1,i);-Ks2(1,i) Ks2(1,i)];']);
end
%% I need to evaluate:
% [X1,e1] = polyeig(K1,M)
% [X2,e2] = polyeig(K2,M) & so on...
% where X represents Eigen-Vectors & e represents Eigen-values
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Run the following code. It will be mush faster as compared to the current code (Remember, eval is evil).
close all
clc
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
m1 = 16500;
m2 = 16100;
M = [m1 0;0 m2];
x1 = linspace(0,3,5000);
x2 = linspace(0,2,5000);
k1 = 2.968888888888889e+07; %(k1=k2)
Ks1 = k1*x1;
Ks2 = k1*x2;
K = cell(size(x1));
for i = 1:length(x1)
K{i} = [Ks1(1,i)+Ks2(1,i) -Ks2(1,i);-Ks2(1,i) Ks2(1,i)];
end
[X, E] = cellfun(@(k) polyeig(k, M), K, 'uni', 0);
Thanks a lot @Ameer Hamza. It really works.
And that was amazing (eval is evil) :)
I am glad to be of help!
I've a MATLAB code to obtain prior PDF plot, as:
%%------------------------------------------------------------------------------------------------
% Prior PDF--
theta1 = linspace(0,3,5000);
theta2 = linspace(0,2,5000);
p0 = lognpdf(theta1,1.3,1).*lognpdf(theta2,0.8,1); % Prior-PDF function
syms t
g1 = (1/t)*exp(-0.5*((log(t)-1.3)^2));
g2 = (1/t)*exp(-0.5*((log(t)-0.8)^2));
a = int(g1*g2,t,0,3);
b = (1/(2*pi()))*double(a);
c = 1/b;
y = c*p0;
figure
plot(theta1,y,'k','LineWidth',2)
grid on;
axis([0 3 0 1])
xlabel('Theta-1');
ylabel('Prior PDF');
title('Prior-PDF function Plot-1');
%%-------------------------------------------------------------------------------------------%%
Now, i want the same results using ksdensity function? How to obtain this?
Can you answer this?

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2014b

Community Treasure Hunt

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

Start Hunting!