Changeset 12046 for issm/trunk-jpl/src


Ignore:
Timestamp:
04/18/12 09:10:45 (13 years ago)
Author:
Mathieu Morlighem
Message:

Fixed matrix transpose

File:
1 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/c/matlab/io/WriteMatlabData.cpp

    r12043 r12046  
    2828                for(int i=0;i<M;i++){
    2929                        for(int j=0;j<N;j++){
    30                                 tmatrix[i*N+j]=matrix[j*M+i];
     30                                tmatrix[j*M+i]=matrix[i*N+j];
    3131                        }
    3232                }
     
    4949
    5050        if(matrix){
    51 
    5251                /*convert to double matrix using Matlab's memory manager*/
    5352                double* tmatrix=(double*)mxMalloc(M*N*sizeof(double));
    5453                for(int i=0;i<M;i++){
    5554                        for(int j=0;j<N;j++){
    56                                 tmatrix[i*N+j]=(double)matrix[j*M+i];
     55                                tmatrix[j*M+i]=(double)matrix[i*N+j];
    5756                        }
    5857                }
     
    6160                mxSetN(dataref,(mwSize)N);
    6261                mxSetPr(dataref,(double*)tmatrix);
    63 
    6462        }
    6563        else{
     
    238236                /*Now transpose the matrix and allocate with Matlab's memory manager: */
    239237                tmatrix_ptr=(double*)mxMalloc(rows*cols*sizeof(double));
    240                 for(i=0;i<cols;i++){
    241                         for(j=0;j<rows;j++){
    242                                 tmatrix_ptr[i*rows+j]=matrix_ptr[j*cols+i];
     238                for(i=0;i<rows;i++){
     239                        for(j=0;j<cols;j++){
     240                                tmatrix_ptr[j*rows+i]=matrix_ptr[i*cols+j];
    243241                        }
    244242                }
     
    299297/*Toolkit*/
    300298/*FUNCTION SetStructureField{{{1*/
    301 void SetStructureField(mxArray* dataref,const char* fieldname,int fieldrows,int fieldcols,double* fieldpointer){
    302 
    303         /*Intermediary*/
    304         int      i1,i2;
    305         mxArray *pfield  = NULL;
    306 
    307         /*Copy field using Matlab's API and transpose*/
    308         double*  fieldcopy=NULL;
    309         if (fieldrows*fieldcols){
    310                 fieldcopy=(double*)mxMalloc(fieldrows*fieldcols*sizeof(double));
    311                 for(i1=0;i1<fieldrows;i1++){
    312                         for(i2=0;i2<fieldcols;i2++){
    313                                 fieldcopy[fieldcols*i1+i2]=fieldpointer[fieldrows*i2+i1];
    314                         }
    315                 }
    316         }
    317 
    318         /*Set matlab field*/
    319         pfield=mxCreateDoubleMatrix(0,0,mxREAL);
    320         mxSetM(pfield,fieldcols);
    321         mxSetN(pfield,fieldrows);
    322         mxSetPr(pfield,fieldcopy);
    323         mxSetField(dataref,0,fieldname,pfield);
    324 }
    325 /*}}}*/
     299void SetStructureField(mxArray* dataref,const char* fieldname,int M,int N,double* fieldpointer){
     300
     301        mxArray* field = NULL;
     302
     303
     304        /*Convert field*/
     305        WriteData(&field,fieldpointer,M,N);
     306
     307        /*Assign to structure*/
     308        mxSetField(dataref,0,fieldname,field);
     309}
     310/*}}}*/
Note: See TracChangeset for help on using the changeset viewer.