Truncating a vector from an addressed element in MatLab

In summary, to construct new vectors x and y starting from a specified element in vector x, you can use the colon (:) operator in MatLab. This allows you to select a range of elements from the original vector.
  • #1
edwrai
1
0
hi,
in MatLab, if you have x and y vectors and want to address an element in vector x, let's say i>=3 from which to construct a new vector x that starts from the addressed element until the last element and at the same time construct a new y vector corresponding to the new x vector?

x=[1 2 3 4 5 6 7]
y=[7 6 5 4 3 2 1]
new x should be=[4 5 6 7]
and new y =[4 3 2 1 ]


Maybe using an array=[x y]?

Any answer would be very much appreciated.
Thanks in advance
 
Physics news on Phys.org
  • #2
Don't know if this is still useful or not, but you'd have to use the colon (:) operator. See the Mathworks page for more information and usage:
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/colon.html

In your case, you'd use the following:
>> x = x(3:5);
>> y = y(3:5);

Once you read through the above page, you'll understand why.
 
  • #3
.


Hello,

Thank you for your question. To truncate a vector from an addressed element in MatLab, you can use the "end" keyword to indicate the last element of the vector. For example, if you want to create a new vector x starting from the third element of the original vector x until the last element, you can use the following code:

x=[1 2 3 4 5 6 7];
new_x = x(3:end); % this will create a new vector starting from the third element until the end

To create a corresponding new y vector, you can use the same indexing as the new_x vector:

y=[7 6 5 4 3 2 1];
new_y = y(3:end); % this will create a new vector starting from the third element of y until the end

Using an array=[x y] will not work in this case because it will simply concatenate the two vectors, resulting in a vector with 14 elements. Instead, you can use the "end" keyword to create the desired truncated vectors.

I hope this helps. Happy coding!
 

1. What does it mean to truncate a vector from an addressed element in MatLab?

Truncating a vector from an addressed element in MatLab means to remove or delete a portion of a vector starting from a specific element.

2. How do I truncate a vector from an addressed element in MatLab?

To truncate a vector from an addressed element in MatLab, you can use the built-in function truncate or the indexing operator () to specify the range of elements you want to keep.

3. Can I truncate a vector from multiple addressed elements in MatLab?

Yes, you can truncate a vector from multiple addressed elements in MatLab by using a range of indices or a logical array to specify the elements you want to keep.

4. Will truncating a vector from an addressed element in MatLab modify the original vector?

Yes, truncating a vector from an addressed element in MatLab will modify the original vector. If you want to keep the original vector, you can assign the truncated vector to a new variable.

5. Are there any limitations to truncating a vector from an addressed element in MatLab?

There are no specific limitations, but it is important to ensure that the indices or logical array used to specify the elements to keep are within the range of the vector to avoid errors.

Similar threads

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