Grab elements from a vector thru formula, matlab

In summary, you want to index a vector by first taking the first n elements and placing them somewhere else, then the next n-1 elements and placing them somewhere else, and so on. However, you need to check some parameters and ensure that you don't go beyond the limits of the vector.
  • #1
brydustin
205
0
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:
Physics news on Phys.org
  • #2
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.
 
  • #3
DiracRules said:
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.
 
  • #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...
 
  • #5
DiracRules said:
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...
 
  • #6
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:
  • #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).
 
  • #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?
 
  • #9
thanks mate! that was really good.
 

What is a vector in Matlab?

A vector in Matlab is a one-dimensional array that can store multiple elements of the same data type. It is often used to represent mathematical or scientific data.

How do I grab elements from a vector in Matlab?

You can use the indexing operator [] to grab elements from a vector in Matlab. For example, if your vector is named "myVector", you can grab the third element by using myVector(3).

Can I grab elements from a vector using a formula in Matlab?

Yes, you can use logical indexing to grab elements from a vector in Matlab using a formula. For example, if you want to grab all elements greater than 5 in a vector named "myVector", you can use myVector(myVector > 5).

Are there any built-in functions in Matlab for grabbing elements from a vector?

Yes, there are several built-in functions in Matlab for grabbing elements from a vector, such as the "find" function and the "ismember" function. These functions can be useful when you want to grab specific elements based on certain criteria.

Can I grab elements from a vector in Matlab and store them in a new vector?

Yes, you can use the indexing operator and logical indexing to grab elements from a vector in Matlab and store them in a new vector. For example, if you want to grab all elements greater than 5 from a vector named "myVector" and store them in a new vector named "newVector", you can use newVector = myVector(myVector > 5).

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
553
  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
Back
Top