Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Grab elements from a vector thru formula, matlab

  1. Aug 10, 2011 #1
    I have a vector, say v = [ 1 2 3 4 5 6 ....] '
    and suppose that I want to first take the first n elements and place them somewhere else, then the next n-1 elements and place them somewhere else, and so on....

    How would I do the indexing if it were performed in a loop?
    So for example, if v = [ 1 2 3 4 5 6 ] '
    then I could do
    a = v(1:3,:)
    b = v(4:5,:)
    c = v(6:6,:) = v(6,:)

    I think that I might need two index values and that they may have to be equated somehow.... not sure, any ideas appreciated.
    essentially I'm looking for a formula for v(i,j)
    Last edited: Aug 10, 2011
  2. jcsd
  3. Aug 10, 2011 #2
    I think the basic code should be like this

    Code (Text):

    a=0   %counter1
    for i=(N-1):-1:1     %not sure about the syntax. Maybe you can also write (N-1):1
      q(N-i-1)= v(a:a+i,:);   %save in vector (or matrix, or whatever you want) elements from a to N-1, that is N elements
      a=i+1;    %this is to remember where to start in the following loop

    However, there are some parameters to be checked, not to go beyond the limit of the vector v.
    I put down some lines more, maybe there are some offsets that have to be adjusted.
    Code (Text):

    for i=(N-1):-1:1
       if (i<=L-a)     %if-else condition to avoid going beyond end of vector. Check it.
    I had not them tested... Hope the code is correct.
  4. Aug 10, 2011 #3
    It doesn't appear correct when working out by hand, and it doesn't work in matlab; even when I adjusted for a=0 (because you can't have a 0 index) it still doesn't work right.
  5. Aug 10, 2011 #4
    sorry for the indexing. It's just that sometimes I get confused with C... there vectors start from 0 :D I'm working on the code...
  6. Aug 10, 2011 #5
    cool.... although perhaps i should have mentioned that the length (dimension) of the vector is equal to the number of entries of an upper triangular (square) matrix. Namely, the data needs to be put in matrix form and we can do that by putting it in the top part and copying it to the bottom through a transpose. Although, I only mention this so that its clear that the length of the vector will always be 3, 6 10, 15, or etc.....
  7. Aug 10, 2011 #6
    Code (Text):

    N=   %you need to put down the dimension of the first vector (e.g., in [1 2 3 4 5 6] then N=3, if [1 2 3 4 5 6 7 8 9 10] then N=4)
    for i=n:-1:0

    This could works (I tried only with v=[1 2 3 4 5 6 7 8 9 10] and N=4)

    Ok. Yes, I noticed that the method of extraction of the elements was linked to triangular numbers :D
    Last edited: Aug 10, 2011
  8. Aug 10, 2011 #7
    Seems working right on vectors you deal with (1, 3, 6, 10, 15 ... in length).

    Obviously, you need to modify the output (I just printed the vectors).
  9. Aug 10, 2011 #8
    If you are sure that the length of the vector is a triangular number, you can use this to evaluate N:

    N=roots([1 1 -2.*L])(2,:)

    The n-th triangular number is given from Gauss' formula T_n={n(n+1)}/2. If T_n is a triangular number, n^2+n-2T=0 has two roots, one positive and one negative.
    roots() gives you both. I put (2,:) to select the second, which should be the positive one.
    However, since I'm using a software similar to matlab but not identical, that way to select the positive solution can be wrong/there could be better ways :D

    Does it work?
  10. Aug 10, 2011 #9
    thanks mate! that was really good.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook