12,815,929 members (40,153 online)
alternative version

#### Stats

56.5K views
17 bookmarked
Posted 3 Apr 2003

# An STL-Like Vector With Matlab Indexing Facilities

, 3 Apr 2003 GPL3
 Rate this:
An STL-Like Vector with Matlab indexing facilities.

## Introduction

Vector is the basic data structure in MATLAB. It provides easy, fast, effective, and scalable data structures giving programming an algebraic view for data, even for complex data structures. Matlab allows you do develop fast technical solutions. However, MATLAB loops, and UIs are too slow :o. It is your responsibility to reformulate your code and data structures into matrices and vectors.

For example, to sum up a vector, you can write a simple counting loop. However, it is faster to use inner product with a vector of ones.

## mtVector, mtRefVector

`mtVector` provides an STL-like vector. Actually, it wraps `std::vector`. Mathematical and indexing operators are added.

```typedef mtVector<int> intVector;
intVector iv1(15,   /* 15 elements */

0,1,2,3,4,5,6,7,8,9,10,11,12,13,14);

intVector iv2(15,   /* 15 elements */

0,10,20,30,40,50,60,70,80,90,100,110,120,130,140);

print(iv1);```

prints:

`iv1= < 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 >`

and,

`print(iv1(4 to 12));`

returns `mtRefVector` and prints:

`iv1(4 to 12)= < 4 5 6 7 8 9 10 11 12 >`

Similarly:

```iv1(4 to 12)=iv2(4 to 12);
print(iv1);```

assigns values in `iv2(4..12)` to elements in `iv1(4..12)`, and prints:

`iv1= < 0 1 2 3 40 50 60 70 80 90 100 110 120 13 14 >`

Moreover, it extends to select elements given a vector of their indices:

```intVector iv3(15, 0,1,2,1,2,0,2,0,1,1,0,4,4,0,4);

print(iv1(iv3.FindOnes()));```

returns `mtRefVector` and prints:

`iv1(iv3.FindOnes())= < 9 18 27 360 540 720 810 990 1080 126 >`

I guess `mtVector` and `mtRefVector` provide simple intuitive way to access and play with vectors. Simply, we can sort `iv1`, `iv2` and `iv3` given index vector of any other sorted vector.

`iv1(iv5.ISort());`

gets indices that sorts `iv5`, and access `iv1` in the same order.

## Share

 Engineer Australia
No Biography provided

## You may also be interested in...

 First Prev Next
 help help help haider prolog1-Aug-06 4:33 haider prolog 1-Aug-06 4:33
 Re: help help help Mohammed Hossny1-Aug-06 16:07 Mohammed Hossny 1-Aug-06 16:07
 How to convert from a STL-vector FinEngineer8-Dec-04 8:03 FinEngineer 8-Dec-04 8:03
 Re: How to convert from a STL-vector Mohammed Hossny8-Dec-04 22:03 Mohammed Hossny 8-Dec-04 22:03
 How using a vector index to select el. FinEngineer1-Dec-04 23:59 FinEngineer 1-Dec-04 23:59
 Re: How using a vector index to select el. Mohammed Hossny2-Dec-04 14:58 Mohammed Hossny 2-Dec-04 14:58
 Re: How using a vector index to select el. FinEngineer2-Dec-04 23:40 FinEngineer 2-Dec-04 23:40
 Re: How using a vector index to select el. Mohammed Hossny5-Dec-04 2:39 Mohammed Hossny 5-Dec-04 2:39
 matlab error yendluriusha16-Dec-03 19:41 yendluriusha 16-Dec-03 19:41
 Boost multiarray library Jonathan de Halleux4-Apr-03 5:01 Jonathan de Halleux 4-Apr-03 5:01
 Re: Boost multiarray library Mohammed Hossny4-Apr-03 5:25 Mohammed Hossny 4-Apr-03 5:25
 Re: Boost multiarray library Jonathan de Halleux4-Apr-03 5:27 Jonathan de Halleux 4-Apr-03 5:27
 Last Visit: 31-Dec-99 19:00     Last Update: 23-Mar-17 8:21 Refresh 1