473,385 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

translation from matlab to C object containing matrices undefinedlength of object

So what i need is this; (I'm very new at this,, programming in C I
mean...)

In matlab I had a while loop, and after each loop was done I added my
resulting matrix to an object. Seeing the loop is conditional to my
results I do not know in advance how many loops I need. The matrices
are Nx * Ny* Nz*6.

In C I put my matrices into 1D arrays. So I thought that if I use a
counter for each time i do a loop and the calloc and realloc function
that would work...But it doesnt seem to be working...Hope one of you
can help me.

This is a sample text which i use to test my programs...

Vnulpoint is my result matrix for which i used pointers. So Varray is
supposed to contain each of the Vnulpoint matrices...

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define nx 2
#define ny 2
#define nz 2

void max_array(double *, int,int *,double *);
int compare_doubles (double *, double *);
void vindminnietnul(double *,double *,int,int* );
void vindmaxnieteen(double *,double *,int ,int*);
main()
{ int i,j,s,kl;
int countergetal=6;
double
gradoud[nx*ny*nz*6]={5.569,6.9987,0.00987,98.7743,1009.8876,0.0009987 };;
double *gradpointeroud,*gradpointernieuw;
double gradnieuw[nx*ny*nz*6];
double *Vnulpoint[nx*ny*nz];
double *Vnulpoint2[nx*ny*nz];
gradpointeroud=gradoud;
gradpointernieuw=gradnieuw;
int counter=1;
double *Varray;
Varray=calloc(1*(nx*ny*nz*6),sizeof(double));
while(counter<100)
{
for(i=0; i<nx; i++)
{ for(j=0; j<ny ; j++)
{ for(s=0; s<nz ; s++)
{
Vnulpoint[i+j*nx
+s*nx*ny]=calloc(countergetal,sizeof(double));
Vnulpoint2[i+j*nx
+s*nx*ny]=calloc(countergetal,sizeof(double));
for(kl=0;kl<countergetal;kl++)
{
*(Vnulpoint[i+j*nx+s*nx*ny]+kl)=0.12369*kl;
*(Vnulpoint[i+j*nx+s*nx*ny]+0)=1;

printf("%f\n ",*(Vnulpoint[i+j*nx+s*nx*ny]
+kl));
}
int maxindex;
double Vmax;
max_array(Vnulpoint[i+j*nx+s*nx*ny],
4,&maxindex,&Vmax);
printf("Het maximum volume is %f op locatie %d
\n",Vmax,maxindex);
Vnulpoint2[i+j*nx+s*nx*ny]=Vnulpoint[i+j*nx
+s*nx*ny];
printf("Het toegewezen volume is %f \t %f \t %f
\n",*(Vnulpoint2[i+j*nx+s*nx*ny]),*(Vnulpoint2[i+j*nx+s*nx*ny]
+1),*(Vnulpoint2[i+j*nx+s*nx*ny]+2));
int teller;
double Vsort[countergetal];
for(teller=0;teller<countergetal;teller++)
{
Vsort[teller]=*(Vnulpoint[i+j*nx+s*nx*ny]+teller);
}
for(kl=0;kl<countergetal;kl++)
{
printf("Vsort %d \t %f\n ",kl,Vsort[kl]);
}

qsort(Vsort,countergetal,sizeof(double),(int(*)(co nst void*,const
void*))compare_doubles);
for(kl=0;kl<countergetal;kl++)
{
printf("Vsort %d \t %f\n ",kl,Vsort[kl]);
}

int indexmaxnieteen;
vindmaxnieteen(Vsort,Vnulpoint[i+j*nx
+s*nx*ny],countergetal,&indexmaxnieteen);
printf("De index is %d\n", indexmaxnieteen);
Varray=(double
*)realloc(Varray,counter*(nx*ny*nz*6)*sizeof(doubl e));
int tel;
for (tel=0;tel<6;tel++)
{
Varray[(counter-1)*(i+j*nx+s*nx*ny+tel*nx*ny*nz)]=*(Vnulpoint[i
+j*nx+s*nx*ny]+tel*nx*ny*nz);

}
printf("Varray is %f \t %f \t %f \n %f \t %f \t %f \n en counter
is %d \n", Varray[(counter-1)*(i+j*nx+s*nx*ny)],Varray[(counter-1)*(i
+j*nx+s*nx*ny+1)],Varray[(counter-1)*(i+j*nx+s*nx*ny
+2)],Varray[(counter-1)*(i+j*nx+s*nx*ny+3)],Varray[(counter-1)*(i+j*nx
+s*nx*ny+4)],Varray[(counter-1)*(i+j*nx+s*nx*ny
+5)],Varray[(counter-1)*(i+j*nx+s*nx*ny+6)],counter);
gradpointernieuw=gradpointeroud;

}
}
}
counter++;
}

}

void max_array(double *a, int num_elements,int *indexptmax,double
*maxpt)
{
int i;

*indexptmax=0;
*maxpt=a[0];

for (i=1; i<num_elements; i++)
{
if (a[i]>*maxpt)
{
*maxpt=a[i];
*indexptmax=i;
}
}
return;
}

int compare_doubles (double *x, double *y)
{

if (*x *y)
{
return 1;
}
else
{
if (*x < *y)
{
return -1;
}
else
{
return 0;
}
}
}

void vindminnietnul(double *V,double *Vl,int counternummer,int *index)
{ int nk,nn;

for(nk=0;nk<counternummer;nk++)
{ if ( *(V+nk)!=0)
{for (nn=0;nn<counternummer;nn++)
{
if (*(Vl+nn-1)==*(V+nk))
{
*index=nn;
}

}
break;
}

}

}

void vindmaxnieteen(double *V,double *Vl,int counternummer,int*index)
{ int nk,nn;

for(nk=0;nk<counternummer;nk++)
{ if ( *(V+(counternummer-1)-nk)!=1)
{for (nn=0;nn<counternummer;nn++)
{
if (*(Vl+nn)==*(V+(counternummer-1)-nk))
{
*index=nn;
}
}
break;
}
}
return;
}
Jun 27 '08 #1
5 1974
In article <8e**********************************@e39g2000hsf. googlegroups.com>,
adinda <ad***************@gmail.comwrote:
>So what i need is this; (I'm very new at this,, programming in C I
mean...)

In matlab I had a while loop, and after each loop was done I added my
resulting matrix to an object. Seeing the loop is conditional to my
results I do not know in advance how many loops I need. The matrices
are Nx * Ny* Nz*6.

In C I put my matrices into 1D arrays. So I thought that if I use a
counter for each time i do a loop and the calloc and realloc function
that would work...But it doesnt seem to be working...Hope one of you
can help me.
What is it doing, and what were you expecting?
Reducing your code to the smallest example that demonstrates the
problem will make it a lot easier for people to help you (and may
also lead you to the problem without external help).
dave

--
Dave Vandervies dj3vande at eskimo dot com
[D]etermining whether [the DS9k2 is] a conforming implementation is
equivalent to solving the halting problem. --Keith Thompson
(Hmm, I wonder if that's true for most real-world implementations.) in CLC
Jun 27 '08 #2
adinda <ad***************@gmail.comwrites:
So what i need is this; (I'm very new at this,, programming in C I
mean...)

In matlab I had a while loop, and after each loop was done I added my
resulting matrix to an object. Seeing the loop is conditional to my
results I do not know in advance how many loops I need. The matrices
are Nx * Ny* Nz*6.

In C I put my matrices into 1D arrays.
That is an off choice. With 4D arrays it leads to code that makes my
head hurt just reading it. If, for some reason, you must do it this
way, at least provide an access function so the code is readable.
So I thought that if I use a
counter for each time i do a loop and the calloc and realloc function
that would work...But it doesnt seem to be working...Hope one of you
can help me.
That is a bit vague. Since I have no idea what it should do, it is
very unlikely that I can help fix it. At least say what is going
wrong.
This is a sample text which i use to test my programs...

Vnulpoint is my result matrix for which i used pointers. So Varray is
supposed to contain each of the Vnulpoint matrices...

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define nx 2
#define ny 2
#define nz 2
I'd add one for the magic number 6 that appears in a few places.
void max_array(double *, int,int *,double *);
int compare_doubles (double *, double *);
void vindminnietnul(double *,double *,int,int* );
void vindmaxnieteen(double *,double *,int ,int*);
main()
{ int i,j,s,kl;
int countergetal=6;
double
gradoud[nx*ny*nz*6]={5.569,6.9987,0.00987,98.7743,1009.8876,0.0009987 };;
double *gradpointeroud,*gradpointernieuw;
double gradnieuw[nx*ny*nz*6];
double *Vnulpoint[nx*ny*nz];
double *Vnulpoint2[nx*ny*nz];
gradpointeroud=gradoud;
gradpointernieuw=gradnieuw;
int counter=1;
double *Varray;
Varray=calloc(1*(nx*ny*nz*6),sizeof(double));
while(counter<100)
Head starting to hurt. Add spaces, please!

Where you have calloc and realloc, you should check the return. In
this short of program, I'd write small array allocate (and
re-allocate) functions that just exit if the allocation fails.
{
for(i=0; i<nx; i++)
{ for(j=0; j<ny ; j++)
{ for(s=0; s<nz ; s++)
{
Vnulpoint[i+j*nx
+s*nx*ny]=calloc(countergetal,sizeof(double));
Vnulpoint2[i+j*nx
+s*nx*ny]=calloc(countergetal,sizeof(double));
for(kl=0;kl<countergetal;kl++)
{
Hmm... a for inside a for inside a for inside for inside a while. I
would try to find some way to decompose these loops into functions.
*(Vnulpoint[i+j*nx+s*nx*ny]+kl)=0.12369*kl;
*(Vnulpoint[i+j*nx+s*nx*ny]+0)=1;

printf("%f\n ",*(Vnulpoint[i+j*nx+s*nx*ny]
+kl));
}
int maxindex;
double Vmax;
max_array(Vnulpoint[i+j*nx+s*nx*ny],
4,&maxindex,&Vmax);
printf("Het maximum volume is %f op locatie %d
\n",Vmax,maxindex);
Vnulpoint2[i+j*nx+s*nx*ny]=Vnulpoint[i+j*nx
+s*nx*ny];
printf("Het toegewezen volume is %f \t %f \t %f
\n",*(Vnulpoint2[i+j*nx+s*nx*ny]),*(Vnulpoint2[i+j*nx+s*nx*ny]
+1),*(Vnulpoint2[i+j*nx+s*nx*ny]+2));
int teller;
double Vsort[countergetal];
for(teller=0;teller<countergetal;teller++)
{
Vsort[teller]=*(Vnulpoint[i+j*nx+s*nx*ny]+teller);
}
for(kl=0;kl<countergetal;kl++)
{
printf("Vsort %d \t %f\n ",kl,Vsort[kl]);
}

qsort(Vsort,countergetal,sizeof(double),(int(*)(co nst void*,const
void*))compare_doubles);
You can loose the cast if you write compare function differently. See
below.
for(kl=0;kl<countergetal;kl++)
{
printf("Vsort %d \t %f\n ",kl,Vsort[kl]);
}

int indexmaxnieteen;
vindmaxnieteen(Vsort,Vnulpoint[i+j*nx
+s*nx*ny],countergetal,&indexmaxnieteen);
printf("De index is %d\n", indexmaxnieteen);
Varray=(double
*)realloc(Varray,counter*(nx*ny*nz*6)*sizeof(doubl e));
You don't need the cast. Also, do you really need to realloc? It
looks to me that know the final size at the start and you could just
allocate once. Of course, I am a bit lost by now, so I may have that
wrong.
int tel;
for (tel=0;tel<6;tel++)
{
Varray[(counter-1)*(i+j*nx+s*nx*ny+tel*nx*ny*nz)]=*(Vnulpoint[i
+j*nx+s*nx*ny]+tel*nx*ny*nz);

}
printf("Varray is %f \t %f \t %f \n %f \t %f \t %f \n en counter
is %d \n", Varray[(counter-1)*(i+j*nx+s*nx*ny)],Varray[(counter-1)*(i
+j*nx+s*nx*ny+1)],Varray[(counter-1)*(i+j*nx+s*nx*ny
+2)],Varray[(counter-1)*(i+j*nx+s*nx*ny+3)],Varray[(counter-1)*(i+j*nx
+s*nx*ny+4)],Varray[(counter-1)*(i+j*nx+s*nx*ny
+5)],Varray[(counter-1)*(i+j*nx+s*nx*ny+6)],counter);
Head really sore now!
gradpointernieuw=gradpointeroud;

}
}
}
counter++;
}

}

void max_array(double *a, int num_elements,int *indexptmax,double
*maxpt)
{
int i;

*indexptmax=0;
*maxpt=a[0];

for (i=1; i<num_elements; i++)
{
if (a[i]>*maxpt)
{
*maxpt=a[i];
*indexptmax=i;
}
}
return;
}

int compare_doubles (double *x, double *y)
{

if (*x *y)
{
return 1;
}
else
{
if (*x < *y)
{
return -1;
}
else
{
return 0;
}
}
}
I'd write:

int compare_doubles(const void *xvp, const void *yvp)
{
const double *xp = xvp;
const double *yp = yvp;
return (*xp < *yp) ? -1 : (*xp *yp);
}
void vindminnietnul(double *V,double *Vl,int counternummer,int *index)
{ int nk,nn;

for(nk=0;nk<counternummer;nk++)
{ if ( *(V+nk)!=0)
You do this a lot. I think V[nk] is much clearer than *(V+nk).
{for (nn=0;nn<counternummer;nn++)
{
if (*(Vl+nn-1)==*(V+nk))
{
*index=nn;
}

}
break;
}

}

}
I think I see now the reason for 1D arrays. You'd like to process
them as above with a single loop rather than 4 nested for loops. I
would not worry about that. It is almost always better to write the
clearest code you can, and then fiddle with it if you need t speed it
up. [You would get away with treating a 4D array as a 1D one (if it
comes to that) on all the compilers you are every likely to meet.]
void vindmaxnieteen(double *V,double *Vl,int counternummer,int*index)
{ int nk,nn;

for(nk=0;nk<counternummer;nk++)
{ if ( *(V+(counternummer-1)-nk)!=1)
{for (nn=0;nn<counternummer;nn++)
{
if (*(Vl+nn)==*(V+(counternummer-1)-nk))
{
*index=nn;
}
}
break;
}
}
return;
}
Serious question: why re-write in C? It is not the obvious choice for
this sort of thing.

--
Ben.
Jun 27 '08 #3
Well actually this is just a test file. In my actual main file i don't
know my count until i've completed the loop (because my magnetic
energy needs to agree with my constraints, and i don't know how many
times I have to do the loop)
I put my 4D arrays in 1D because my collegues do that too and then we
can interchange code.
The reason why i'm translating in C is because matlab is slow. and C
is supposed to be faster (I hope)

Thanks for the tips,

Adinda
Jun 27 '08 #4
It helps if you quote a bit of the reply you are commenting on.

adinda <ad***************@gmail.comwrites:
Well actually this is just a test file. In my actual main file i don't
know my count until i've completed the loop (because my magnetic
energy needs to agree with my constraints, and i don't know how many
times I have to do the loop)
OK, so you probably need realloc.
I put my 4D arrays in 1D because my collegues do that too and then we
can interchange code.
An excellent reason, though they should reconsider as well!
The reason why i'm translating in C is because matlab is slow. and C
is supposed to be faster (I hope)
OK. I meant why C specifically. Fortran used to be the language of
choice for such things but if your colleagues have chosen it, you must
follow.
Thanks for the tips,
There are unlikely to be any more unless you can say (in way that is
not domain specific) what was wrong with the program you posted.

--
Ben.
Jun 27 '08 #5
Ben Bacarisse wrote, On 25/04/08 15:28:
adinda <ad***************@gmail.comwrites:
<snip>
> printf("Varray is %f \t %f \t %f \n %f \t %f \t %f \n en counter
is %d \n", Varray[(counter-1)*(i+j*nx+s*nx*ny)],Varray[(counter-1)*(i
+j*nx+s*nx*ny+1)],Varray[(counter-1)*(i+j*nx+s*nx*ny
+2)],Varray[(counter-1)*(i+j*nx+s*nx*ny+3)],Varray[(counter-1)*(i+j*nx
+s*nx*ny+4)],Varray[(counter-1)*(i+j*nx+s*nx*ny
+5)],Varray[(counter-1)*(i+j*nx+s*nx*ny+6)],counter);

Head really sore now!
<snip>

I'm not surprised. However, when poked gcc gives the following warning
for this line...
t.c:94: warning: format ‘%d’ expects type ‘int’, but argument 8 has type
‘double’
So I suspect the OP has either too many parameters or too fe format
specifiers.

It also pointed out that Vsort is a variable sized array. In the full
code this may or may not be appropriate, but as far as I can see in the
example countergetal (which specifies the size of this array amongst
other things) is set to 6 and never changed.
--
Flash Gordon
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mathias | last post by:
Dear NG, I currently ty to switch from matlab to python/scipy but have a lot of trouble with images. What I need is a function for subsequently displaying a number of 2D-matrices as an image. I...
4
by: dataangel | last post by:
I'm a student who's considering doing a project for a Machine Learning class on pathing (bots learning to run through a maze). The language primarily used by the class has been Matlab. I would...
2
by: Binod Pant | last post by:
Does any one know of a way to call MATLAB from PHP/Perl? I found the Perl module Math:MATLAB , but it seems not to work on Windows, and it's not easy to install and use. This would enable one to...
8
by: Allen | last post by:
Does anyone agree with me? If you have used Matlab, welcome to discuss it.
2
by: zipls | last post by:
suppose a simple matlab function //add.m function result=add(left,right) result=left + right; //end of add.m use matlab comtool to encapsulate the function in fun.dll and then use it in...
2
by: eric_jin | last post by:
i called function show() in a c# webservice //show.m function ans=show(x) ans=x; it works; but when i try to call add(),it breaks; //add.m
103
by: aboxylica | last post by:
hey! I have a program that takes two input files(one in the matrix form) and one in the sequence form.Now my problem is that i have to give the matrix file(containing many matrices) and sequence...
1
by: =?Utf-8?B?aGFrYW5zaWx2ZXI=?= | last post by:
Dear all, We try to access a third party Matlab dll (which was created using Matlab COM Builder) from C#. It works fine when using strings and one-dimensional arrays but fails with...
0
by: adinda | last post by:
Well actually this is a smaller example than my main text to be honest... What i need to find is a way to save the resulting matrix Vnul, pointed to by Vnulpoint in an array of sorts. But i need...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.