MATLAB Grab elements from a vector thru formula, matlab

  • Thread starter brydustin
  • Start date
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)
Thanks
 
Last edited:
I think the basic code should be like this

Code:
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
end.

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:
L=size(v,1);
a=0;
for i=(N-1):-1:1
   if (i<=L-a)     %if-else condition to avoid going beyond end of vector. Check it.
        q(N-i-1)=v(a:a+i,:);
        a=i+1;
   else
       q(N-i-1)=v(a:L,:);
   end;
end.
I had not them tested... Hope the code is correct.
 
I think the basic code should be like this

Code:
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
end.

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:
L=size(v,1);
a=0;
for i=(N-1):-1:1
   if (i<=L-a)     %if-else condition to avoid going beyond end of vector. Check it.
        q(N-i-1)=v(a:a+i,:);
        a=i+1;
   else
       q(N-i-1)=v(a:L,:);
   end;
end.
I had not them tested... Hope the code is correct.
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.
 
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...
 
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...
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.....
 
Code:
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)
L=size(v,2)
a=1;
n=N-1;
for i=n:-1:0

        v(:,a:a+i)
        a=a+i+1;
end
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:
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).
 
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?
 
thanks mate! that was really good.
 

Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving
Top