Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how assign a matrix to a variable? Can the variable be a matrix variable?

Tags:

matlab

I was trying to use for loop to assign some matrix to some variables. But I could not realize it. I probably know where is my mistake, but I don't know whether there is a way to overcome it


N = 10;

for i = 1:1:N

    P(i) = [x(i)^2   x(i)*y(i);  
        x(i)*y(i)   y(i)^2];
end 

K = blkdiag(P);

I want to assign a matrix to P(i), then use those P(i) to create a block diagonal matrix. But it seems that I can't do this. Is there any other methods to create such block diagonal matrix?

like image 818
user1698906 Avatar asked Mar 07 '26 11:03

user1698906


1 Answers

You can use a cell array for this:

for i = 1:10
    P{i} = [x(i)^2   x(i)*y(i);  
            x(i)*y(i)   y(i)^2];
end
K = blkdiag(P{:});
like image 70
Ben Voigt Avatar answered Mar 10 '26 07:03

Ben Voigt