Click here to Skip to main content
Licence GPL3
First Posted 3 Apr 2003
Views 48,240
Bookmarked 17 times

An STL-Like Vector With Matlab Indexing Facilities

By | 3 Apr 2003 | Article
An STL-Like Vector with Matlab indexing facilities.

Sample Image - mtVector.jpg

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.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

About the Author

Mohammed Hossny

Other

Australia Australia

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalhelp help help Pinmemberhaider prolog3:33 1 Aug '06  
AnswerRe: help help help PinmemberMohammed Hossny15:07 1 Aug '06  
QuestionHow to convert from a STL-vector PinmemberFinEngineer7:03 8 Dec '04  
AnswerRe: How to convert from a STL-vector PinmemberMohammed Hossny21:03 8 Dec '04  
QuestionHow using a vector index to select el. PinmemberFinEngineer22:59 1 Dec '04  
AnswerRe: How using a vector index to select el. PinmemberMohammed Hossny13:58 2 Dec '04  
GeneralRe: How using a vector index to select el. PinmemberFinEngineer22:40 2 Dec '04  
GeneralRe: How using a vector index to select el. PinmemberMohammed Hossny1:39 5 Dec '04  
Generalmatlab error Pinmemberyendluriusha18:41 16 Dec '03  
GeneralBoost multiarray library PinmemberJonathan de Halleux4:01 4 Apr '03  
GeneralRe: Boost multiarray library PinmemberMohammed Hossny4:25 4 Apr '03  
GeneralRe: Boost multiarray library PinmemberJonathan de Halleux4:27 4 Apr '03  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 4 Apr 2003
Article Copyright 2003 by Mohammed Hossny
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid