how to get any element from a two dimensional set Topic is solved

Problems with modeling
Post Reply
svechias
User
User
Posts: 3
Joined: 7 months ago

how to get any element from a two dimensional set

Post by svechias » 7 months ago

Hi,

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/;
How may I get the first, second or third element from set a by ordinality?

I wanted to use

Code: Select all

ord(a)
but it didn't work with two dimensional set.

Freddy
User
User
Posts: 11
Joined: 2 years ago

Re: how to get any element from a two dimensional set

Post by Freddy » 7 months ago

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;

svechias
User
User
Posts: 3
Joined: 7 months ago

Re: how to get any element from a two dimensional set

Post by svechias » 7 months ago

Freddy wrote:
7 months ago
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.
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 ;
);
the b(i,j) result will be 2.3 .

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

User avatar
dirkse
Moderator
Moderator
Posts: 121
Joined: 3 years ago
Location: Fairfax, VA

Re: how to get any element from a two dimensional set

Post by dirkse » 7 months ago

You can compute your ord value yourself with a loop:

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;
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

svechias
User
User
Posts: 3
Joined: 7 months ago

Re: how to get any element from a two dimensional set

Post by svechias » 7 months ago

dirkse wrote:
7 months ago
You can compute your ord value yourself with a loop:

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;
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
Thank you. It works.

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.

Post Reply