3

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?

1 Answer 1

5

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{:});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.