2

I am trying to make a loop which will create a matrix of appropriate size. I need the matrix to be constructed of a specified number of elements which are pulled from another matrix and added together.

Here is some psuedo code:

    '

        n=10
        for x=1:1:n

            Kglobal=zeros(nxn)


        Kglobal(1,1)=1
        Kglobal(x+1,x+1)=Klocalx(2,1)
        Kglobal(x+1,x+2)=Klocalx(2,2)+Klocalx+1(1,1)'
if Kglobal(x+1,x+2)=(n+1,n)
   Kglobal(n,n)=Klocaln(2,2)
    end
end

When finished resulting in something like:

'  
 Kglobal=[ 1 0 0 0 0 0 0 0 0 0 0;
        Klocal1(2,1) Klocal1(2,2)+Klocal2(1,1) 0 0 0 0 0 0 0 0 0;
        0 Klocal2(2,1) Klocal2(2,2)+Klocal3(1,1) 0 0 0 0 0 0 0 0;
        0 0 Klocal3(2,1) Klocal3(2,2)+Klocal4(1,1) 0 0 0 0 0 0 0;
        0 0 0 Klocal4(2,1) Klocal4(2,2)+Klocal5(1,1) 0 0 0 0 0 0;
        0 0 0 0 Klocal5(2,1) Klocal5(2,2)+Klocal6(1,1) 0 0 0 0 0;
        0 0 0 0 0 Klocal6(2,1) Klocal6(2,2)+Klocal7(1,1) 0 0 0 0;
        0 0 0 0 0 0 Klocal7(2,1) Klocal7(2,2)+Klocal8(1,1) 0 0 0;
        0 0 0 0 0 0 0 Klocal8(2,1) Klocal8(2,2)+Klocal9(1,1) 0 0;
        0 0 0 0 0 0 0 0 Klocal9(2,1) Klocal9(2,2)+Klocal10(1,1) 0;
        0 0 0 0 0 0 0 0 0 Klocal10(2,1) Klocal10(2,2)];
'

Thanks!! I appreciate any help and will be checking frequently so if there is something I can try to explain better please let me know!

4
  • Klocal would be a 2x2 matrix and the values are based on the calculation I am performing for each point in the matrix. I can provide that but for example (2,1) would simply be a value like Klocal(2,1)=-110.
    – user618204
    Mar 30, 2011 at 2:55
  • so Klocal would look like [100,-110; -110,1200]
    – user618204
    Mar 30, 2011 at 2:56
  • If it helps the values of that matrix are calculated invdividually...so maybe I could make them a function of x? Currently it is set up like K11=eqn K12=eqn K21=eqn K22=eqn so maybe there is way to make those a function of the counter so I can call the right equation?
    – user618204
    Mar 30, 2011 at 3:02
  • 1
    You might want to make your KlocalNN matrices part of a cell array instead, then you can iterate over them: Klocal{1} = fun(1, ..); Klocal{2} = fun(2, ...); This is cleaner than appending numbers to variable names.
    – Alex
    Mar 30, 2011 at 3:10

1 Answer 1

1

To create a matrix like that is quite simple using the spdiags function. For example,

a=1:5;
b=6:10;
c=spdiags([a',b'],[-1,0],5,5);

creates a sparse matrix that looks like this

 6     0     0     0     0
 1     7     0     0     0
 0     2     8     0     0
 0     0     3     9     0
 0     0     0     4    10

except that memory is allocated only for the non-zero elements. This should answer your question as to how to create such a matrix.

However, the question is, how do you get the a's and b's, which in your case are the bunch of Klocal matrices. It is not very clear as to what you're doing in your code. I'm guessing that they can be done as some compact matrix/cell operations, but cannot comment further with what's given.

Anyway, assuming you already have the Klocal matrices, this should be sufficient to get you started.

1
  • Thank you so much. You were correct it can be done using cells. Alex explained how that would work. I am fairly new to Matlab programming. I have used it much more for calculations and simulink.
    – user618204
    Mar 30, 2011 at 4:37

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.