konova
konova

Reputation: 35

How i can implement this Matlab code in C?

I need your help to implement this Matlab code in C.

ro=sqrt((c1.c1).(c2.c2).(c3.*c3));

I know .* is element by element.

Any idea ?

Thank you.

Upvotes: 1

Views: 52

Answers (2)

konova
konova

Reputation: 35

Thank you for your advice. I just change + to * but it's perfect. Thanks!

double C1[2048]={1,2,3,5,6,7,4,2,5};
double C2[2048]={1,6,3,5,6,2,4,1,6};
double C3[2048]={1,2,1,5,6,4,4,2,2};

int ro[2048] = { 0 };

for (int i = 0; i < 2048; i++) {

    ro[i] = sqrt((C1[i]*C1[i]) * (C2[i]*C2[i]) * (C3[i]*C3[i]));
}

Upvotes: 0

davidhood2
davidhood2

Reputation: 1367

This could be solved using a for loop to do the elementwise multiplication. I've provided a very simple solution below, although you would have to populate the contents of C1, C2 and C3 yourself

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void) {
    double C1, C2, C3[2048] = { 0 }; //Initialise arrays
    double ro[2048] = { 0 };

    for (int i = 0; i < 2048; i++) { //Iterate through elementwise

        ro[i] = sqrt(C1[i] ^ 2 + C2[i] ^ 2 + C3[i] ^ 2);

    }
}

Upvotes: 2

Related Questions