Search code examples
modelicadymolaopenmodelica

Acessing a row in a matrix with comparing the values of the first column


I want to access a specific row of a matrix by comparing a user defined parameter to the values of the first column.

Pseudo Code For example:

parameter Real userinput;
Real matrix[4,10] = [10,1,3,5; 3,1,5,9;.....];
Integer rowidentity;

for i in 1:10 loop
 if matrix[1,i] = userinput then
   i = rowidentity;
 end if;
end for;

From what i know for loops or if statements dont work outside the equation part.

How can i do this task without if or for loops?


Solution

  • Write a function that does this returning i. The matrix as input. Then use it in an equation section.

    function getIndex
      input Real userinput;
      input Real matrix[4,10] = [10,1,3,5; 3,1,5,9;.....];
      output Integer rowidentity;
    algorithm
    for i in 1:10 loop
     if matrix[1,i] == userinput then
       rowidentity := i;
       return;
     end if;
    end for;
    end getIndex;