Find the index of an element that equal * in a matrix

For questions regarding the import, export and manipulation of data in EViews, including graphing and basic statistics.

Moderators: EViews Gareth, EViews Jason, EViews Steve, EViews Moderator

NikitaG
Posts: 10
Joined: Wed Sep 28, 2016 12:45 pm

Find the index of an element that equal * in a matrix

Postby NikitaG » Mon Oct 17, 2016 8:25 am

Is there a command that finds the index of a certain element that equals some value from a matrix (or a vector, or series).
Or the index of the row in which column1 = some value and column2 = some other value.

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 11869
Joined: Tue Sep 16, 2008 5:38 pm

Re: Find the index of an element that equal * in a matrix

Postby EViews Gareth » Mon Oct 17, 2016 8:38 am

There is not. You'll have to loop through the elements.
Follow us on Twitter @IHSEViews

EViews Glenn
EViews Developer
Posts: 2598
Joined: Wed Oct 15, 2008 9:17 am

Re: Find the index of an element that equal * in a matrix

Postby EViews Glenn » Mon Oct 17, 2016 9:38 am

I think there are some tricks you can do to make it a bit easier. We'll do an example with vector objects since it's easier, you can do similar things with matrices, using the @vec and @vech and a tiny bit of math.

Here, we create a 10 element vector A and fill it with random normals, but then set A(3)=2. To find the index element containing that value, we can use the @imin function on the squares of the deviations from the desired value. We can then look up the value to make certain that it meets the test.

vector(10) a
nrnd(a)
a(3)=2

!minid =@imin(@epow(a-2,2))
!minval = a(!minid)

For matrices, you can either loop over the columns, or better yet, use the @vec operator to convert into a matrix, then find the minimum vec index and convert that index back into matrix indices.

Note that in all of these cases, the identified value is not unique if there is more than one value meeting the test criterion.

Many other things can be done as a variant of the basic idea.

NikitaG
Posts: 10
Joined: Wed Sep 28, 2016 12:45 pm

Re: Find the index of an element that equal * in a matrix

Postby NikitaG » Wed Oct 19, 2016 9:48 pm

Thank you!

EViews Glenn
EViews Developer
Posts: 2598
Joined: Wed Oct 15, 2008 9:17 am

Re: Find the index of an element that equal * in a matrix

Postby EViews Glenn » Thu Nov 03, 2016 2:52 pm

One thing to note: this will give you the index of the closest value so there will be a match, even if there is no value that is equal.


Return to “Data Manipulation”

Who is online

Users browsing this forum: No registered users and 17 guests