Truncating a vector from an addressed element in MatLab

Click For Summary
SUMMARY

In MatLab, to truncate vectors starting from a specified index, the colon operator (:) is utilized. For example, given vectors x = [1 2 3 4 5 6 7] and y = [7 6 5 4 3 2 1], to create new vectors starting from the third element, the commands x = x(3:end) and y = y(3:end) should be executed. This effectively constructs new vectors x = [4 5 6 7] and y = [4 3 2 1]. Refer to the Mathworks documentation for further details on the colon operator.

PREREQUISITES
  • Understanding of MatLab syntax and vector manipulation
  • Familiarity with the colon operator in MatLab
  • Basic knowledge of indexing in arrays
  • Access to Mathworks documentation for reference
NEXT STEPS
  • Explore advanced vector operations in MatLab
  • Learn about array concatenation techniques in MatLab
  • Investigate the use of logical indexing in MatLab
  • Review the Mathworks documentation on array slicing and indexing
USEFUL FOR

MatLab users, data analysts, and anyone involved in numerical computing who needs to manipulate and truncate vectors efficiently.

edwrai
Messages
1
Reaction score
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
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.
 

Similar threads

  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
5
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 3 ·
Replies
3
Views
5K