The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 LabTalk Forum
 accessing elements in a matrix
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

compagnon@1

France
7 Posts

Posted - 10/31/2002 :  04:22:49 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
what is the best way (the speedest !!!) to put values in a matrix ?
1) MyMatrix!cell(X, Y) = value
2) MyMatrix[Ncolumns*(X-1)+Y] = value

I use this assignment in a loop. I suppose this should be speedest whith OriginC but don't find the way to access matrix elements. Is it possible ?

Thank you for your help.
Isabelle

easwar

USA
1965 Posts

Posted - 10/31/2002 :  09:43:47 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Isabelle,

In LabTalk, you can refer to matrix elements with two different commands:
Matrix1[i]=
or
cell(irow,icol)=
In the first case, the matrix is accessed as an 1D array so you need to compute the correct index for the (i,j)th cell. In the second case, you can directly refer to the cell you want to address. In this case the matrix window has to be active.

In either case, you will need to write a loop. Of course, as you mentioned, such loops are much faster in Origin C - up to a factor of 20 faster. If you look at the Matrix->Set Values dialog, there is an Origin C check box, which makes setting values faster.

You could try code like below to set your matrix values using Origin C. Compile this code, make the matrix window active, and type FillMat in the script window.

Also note that vector operations are possible as well - not necessary to always use loops.
For example, in LabTalk, you can set all values of a matrix to a constant or expression, set all values from a dataset, such as:
Matrix1 = sin(pi);
Matrix1 = Data1_B;
And in Origin C you can set all matrix values to constant or expression, and also set values in matrix from Dataset or Vector using the SetByVector method.

Easwar
OriginLab.


void FillMat()
{
MatrixLayer ml; // Declare OC MatrixLayer object
ml = (MatrixLayer) Project.ActiveLayer(); // Get active MatrixLayer from OC Project object
if(!ml.IsValid())
{
printf("Active page is not a matrix!\n"); // If active page is not a matrix, quit
return;
}
else
{
Matrix matA(ml); // Declare OC Matrix object
int iNumRows = matA.GetNumRows();
int iNumCols = matA.GetNumCols();
for(int ir = 0; ir < iNumRows; ir++)
for(int ic = 0; ic < iNumCols; ic++)
matA[ir][ic] = sin(ir)*cos(ic); // Fill values
}
}


Edited by - easwar on 10/31/2002 2:43:25 PM
Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000