I have a set:

Code: Select all

```
Set i /1*3/;
set j /1*3/;
Set a(i,j) /1.2 2.3 1.3/;
```

I wanted to use

Code: Select all

`ord(a)`

Hi,

I have a set:
How may I get the first, second or third element from set a by ordinality?

I wanted to use but it didn't work with two dimensional set.

I have a set:

Code: Select all

```
Set i /1*3/;
set j /1*3/;
Set a(i,j) /1.2 2.3 1.3/;
```

I wanted to use

Code: Select all

`ord(a)`

How about this?

Code: Select all

```
Set i /1*3/;
set j /1*3/;
Set a(i,j) /1.2,2.3,1.3/;
set b(i,j);
b(i,j)$(ord(i) = 3 and ord(j) = 1) = yes;
```

That's a good idea.Freddy wrote: ↑4 weeks agoHow about this?Code: Select all

`Set i /1*3/; set j /1*3/; Set a(i,j) /1.2,2.3,1.3/; set b(i,j); b(i,j)$(ord(i) = 3 and ord(j) = 1) = yes;`

But I need to put it in the loop so that at the second set c element:

Code: Select all

```
Set i /1*3/;
set j /1*3/;
Set a(i,j) /1.2,2.3,1.3/;
set b(i,j);
set c/1*3/;
loop (c,
b(i,j) $ (ord(a(i,j))=c) = yes ;
);
```

And the code I wrote above does not do this because ord does not work with two-dimensional set.

You can compute your ord value yourself with a loop:

Be careful how you use this, though. If you want to do something for the tuples in a(i,j), then it is best to do that directly, not by a loop over something else (say, a loop from 1 to card(a)) and some code to do something for the (i,j) tuples corresponding to the loop variable. The latter approach may work, but it will not be efficient as the data gets large.

-Steve

Code: Select all

```
Set i /1*3/;
set j /1*3/;
Set a(i,j) /1.2, 2.3, 1.3/;
parameter ordOfA(i,j);
scalar cnt / 0 /;
loop{a(i,j),
cnt = cnt + 1;
ordOfA(i,j) = cnt;
};
display a, ordOfA;
```

-Steve

Thank you. It works.dirkse wrote: ↑3 weeks agoYou can compute your ord value yourself with a loop:

Be careful how you use this, though. If you want to do something for the tuples in a(i,j), then it is best to do that directly, not by a loop over something else (say, a loop from 1 to card(a)) and some code to do something for the (i,j) tuples corresponding to the loop variable. The latter approach may work, but it will not be efficient as the data gets large.Code: Select all

`Set i /1*3/; set j /1*3/; Set a(i,j) /1.2, 2.3, 1.3/; parameter ordOfA(i,j); scalar cnt / 0 /; loop{a(i,j), cnt = cnt + 1; ordOfA(i,j) = cnt; }; display a, ordOfA;`

-Steve

I understand that directly specifying the (i, j) tuples is more efficient. But this is the only way I can get rid of specifying over 50 of tuples and to avoid mechanical work in the future.

This way you only need an extra loop with the set of 50 elements.