Help - Search - Members - Calendar
Full Version: remove a value from array
Modelica Forum > Modelica > Modelica
daniel@ka
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
liuliu
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

Roland
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
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.