Saarland University
Lehrstuhl für Automatisierungstechnik

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> remove a value from array
Roland
post Apr 7 2010, 11:01 PM
Post #1


Advanced Member
***

Group: Members
Posts: 30
Joined: 2-January 08
Member No.: 5,272



Hello,

maybe this is still of interest for you or anyone else...

Since the "1:N" part of the for statement defines the array of values "i" is going to use, you just have to construct an array for "j" that avoids the value of "i".
An example: You want to define a matrix with zeros on the main diagonal and "i*j" for the other values you can write:

CODE
model MatrixTest
  parameter Integer size = 4;
  Real[size, size] x;
equation
  for i in 1:size loop
    for j in cat(1, {k for k in 1:i - 1}, {k for k in i + 1:size}) loop
      x[i, j] = i*j;
    end for;
    x[i, i] = 0;
  end for;
end MatrixTest;


As short explanation: The "cat" command as I used it here creates an array that concatenates all values in the first dimension of the array with values {1, 2, ..., i - 1} and the array with values {i + 1, i + 2, ..., size}.

Since this is in the equation section, you still have to specify equations for the main diagonal ("x[i, i] = 0").

I hope this helps,
Roland
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
liuliu
post Dec 1 2009, 11:14 AM
Post #2


Member
**

Group: Administrators
Posts: 24
Joined: 21-November 06
Member No.: 115



I am not sure whether my solution works, try this out

CODE

for i in 1:N loop
for j in 1:N loop
if i<>J then

// do stuff
end if;

end for;
end for;


since there is no else part, try to write this in algorithm

Regards

QUOTE(daniel@ka @ Nov 19 2009, 09:55 PM) *

Hi,

is there a possibility to remove a value from an array?

the background of the need is following

I have 2 nested for loops in the equation section, and I dont want to allow (i,i) pairs

for i in 1:N loop
for j in 1:N loop
if i==j then continue; // C like description which is not available
// do stuff
end for;
end for;

what would be possible is ...

for i in 1:N loop
for j in 1:i-1,i+1:N loop
// do stuff
end for;
end for;

but now I must somehow exclude i from the array in the nested loop.
Have somebody an idea how this can be done?

thx
daniel

User is offlineProfile CardPM
Go to the top of the page
+Quote Post
daniel@ka
post Nov 19 2009, 08:55 PM
Post #3


Member
**

Group: Members
Posts: 23
Joined: 3-June 08
Member No.: 11,399



Hi,

is there a possibility to remove a value from an array?

the background of the need is following

I have 2 nested for loops in the equation section, and I dont want to allow (i,i) pairs

for i in 1:N loop
for j in 1:N loop
if i==j then continue; // C like description which is not available
// do stuff
end for;
end for;

what would be possible is ...

for i in 1:N loop
for j in 1:i-1,i+1:N loop
// do stuff
end for;
end for;

but now I must somehow exclude i from the array in the nested loop.
Have somebody an idea how this can be done?

thx
daniel
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



Lo-Fi Version Time is now: 16th July 2019 - 10:03 AM