Need help translating MATLAB code to C - c

I have trouble translating the following code into C, as the format for array indexing in C slightly differs from MATLAB. If I could get some pointers (get it?) as how to translate the following code, I'd greatly appreciate it. I am only stuck at the following lines, as the indexing in C won't allow me to do it as follows:-
b = a(x:x+1, y:y+1);
a(x:x+1, y:y+1) = b;
My code:-
a = [1 2 3 4; 2 3 4 5; 3 4 5 6; 4 5 6 7];
c = dctmtx(2);
q_mtx = [16 11; 12 12];
for x = 1:2:M
for y = 1:2:N
b = a(x:x+1, y:y+1); %<----HOW DO I DO THIS IN C???
b = c*b*c';
b = b./q_mtx ;
a(x:x+1, y:y+1) = b;
end
end
Your help is greatly appreciated! Thanks!

As far as I know, there is no build-in operators for subscripting the arrays.
So, you can either look up for suitable libraries (I think, for 2D arrays some Image processing libraries can work, like OpenCV), or write your one code.
The line you are interested in is easy to translate into C.
If in Matlab you have b = a(x:x+1, y:y+1);, in C it would be four lines:
b[0][0]=a[x][y];
b[1][0]=a[x+1][y];
b[0][1]=a[x][y+1];
b[1][1]=a[x+1][y+1];
Here I assume that you defined arrays statically (as you wrote in comments).
This is an advantage of the Matlab - it is really convenient to work with matrices.
Edit: regarding your question in comments, for bigger size you can use for loops:
Matlab code b = a(x:x+32, y:y+55) would correspond to
for(int i=0;i<32;i++)
for(int j=0;j<55;j++)
b[i][j]=a[x+i][y+j];

Related

How transfer an array of integers from C to Matlab

Is there a somewhat easy way to transfer an array of integers from a C program to matlab? I have found several guides online on how to do it between C++ and Matlab, but not for C to Matlab.
I have several arrays of 1180 floats that I produce with a program written in C. Now I want to visualize this data in Matlab. I could have the C program export the Matlab code necessary to create the arrays hard-coded, but this seems unnecesarily devious.
I could just use the C++ method and compile my C program with a C++ compiler but at this point I am just curious if it can also be done in C.
Matlab can import .mat files, but these files seem to be encrypted or in binary format.
Please don't give suggestions on how to visualize the data in C, I have to do it in Matlab. I have a piece of code in Matlab that is returning weird results and I wrote the equivalent code in C, now I want to see if there is a difference in results. This way I can debug my Matlab code, since the end result has to be handed in in Matlab code.
My C code so far. Also I made a mistake in my initial upload, I want to transfer an array of integers.
FILE *f = fopen("Ps.bin", "wb");
for(int n = 1; n < N + 1; n++)
{
converter.asInt = Ps[n];
for(int i = 0; i < 4; i++)
{
fwrite(&converter.asBytes[i], sizeof(char), 1, f);
}
}
fclose(f);
This is what I tried in matlab, but none of it gives the proper result. In all cases matlab makes an array of doubles, which I definately don't want. It just generates an array with values that are one of these three: 0, 0.0000 and 4.925.
Ps = fread(fopen('Ps.bin'))
Ps = fread(fopen('Ps.bin'), 1180)
Ps = fread(fopen('Ps.bin'), 1180, 'uint32')
Ps = fread(fopen('Ps.bin'), 1180, '2 * uint16')
Write a binary file in C (using fwrite, look here). From matlab, you can read it using the procedure here (freadfunction)

Storing Dynamically in an array python

I am actually translating a matlab script into python an i have a problem using arrays in python (I am still a beginner) numpy.
My question is this:
In matlab I am computing the fourier transform of several signals and I am storing dynamically it in a 3 by 3 array say U. A simple example of what i want to do is as follows;
l = 3 ;
c = 0 ;
for i = 1:3
for j = 1:10
c=c+1 ;
a = j + 1;
U(i,c,:)=a ;
end
end
I want to translate this to python and I am unable to create the array U that stores dynamically the value of 'a' in U.
Note : Here am computing 'a' as j+1 for simplicity but in my script 'a' is an array (the fourier transform of a signal)
Sorry for my bad english, I am french. T
I believe you will ultimately want something like this. One of the things that was confusing was what your loop variable c and j were doing. It seems like you wanted c=j, so I changed that below. The one thing you need to watch out for is that python objects are indexed from 0, whereas Matlab objects are index from 1. So below, if you actually start examining the values of i and j, you will see that they start from 0.
import numpy
L = 3;
C = 10;
N = 50; # Size of the Fourier array
U = numpy.zeros((L,C,N))
for i in range(L):
for j in range(C):
# Create a matrix of scalars, for testing
a = i*j*numpy.ones((N,));
U[i,j,:] = a;

C: what does the notation x=y[a=b] imply?

I am trying to rewrite a piece of C code from another author into Python, and I came across the following type of notation:
x = y[a=b];
where y is an array and a and b are integers. At first I thought it meant something like this:
a = b;
x = y[a];
But apparently it doesn't. Can anyone tell me what this expression means? Forgive me if this question is a duplicate, but it is very hard to summarise this question in a couple of searchable keywords, and I could not find anything that answers my question.
The source of the code I am trying to rewrite: link
EDIT:
The Python-rewritten code does not work as it should (stuck in a loop), so I thought that I misinterpreted the above statement. But as several of you have suggested, it was correct in the first place. I must be something else then...
x = y[a=b];
and
a = b;
x = y[a];
are equivalent. The expression a=b has the value of the left operand after assignment.
http://msdn.microsoft.com/en-us/library/474dd6e2.aspx
I think you are actually right, it means:
a = b;
x = y[a];
a = b itself returns b after assigning b to a. Therefore y[a=b] will return y[b] but also assigned b into a.
EDIT
in your comment you say, "if a = 1 and b = 3, then it resolves to y[3] with a = 3 and b = 3".
This is exactly what the code above does:
At the beginning, a is equal to 1 and b is equal to 3.
a = b;
Now a contains 3 and b still contains 3.
x = y[a];
Now x has been assigned the value in y[3], a still contains 3 and b still contains 3.

How to build an array by function handle

I am trying to produce something like this in MATLAB with function handle
f=#(x,y)(x(1)*x(2)+y);
c=[2 3 4;5 9 2];
h=[5 1 2];
f(c,h)
The answer should be:
15 11 12
But when I write this code below, it just builds a number not an array.
f=#(x)(x(1)*x(2))
f(c)
answer:
10
Can someone explain me where I went wrong?
I do not know what you expected here. The cause of the problem is quite clear.
a = 1;
b = 2;
c = [3 4];
d = a*b+c;
is a scalar + vector operation which always returns
ans = [a*b+c(1), a*b+c(2)];
however scalar*scalar which was the second case always returns a scalar. What you do is that you multiply the first matrix element of x (or c) with the second element. That is to say element c(1,1)*c(2,1) since matlab works columnwise. If you looks at your values you would probably notice the answer is incorrect as well, if you are trying to do what I think you are. You could try this instead,
f=#(x,y)(x(1,:).*x(2,:)+y);
c=[2 3 4;5 9 2];
h=[5 1 2];
f(c,h)
which multipies the elements on the first row of x with the same column on the second row and then adds y. An anonymous function takes a number of inputs and perform a defined operation, the same as ordinary functions or ordinary codes. You can see them as functions that does not require a call to another m file. The main differences (except that ordinary functions gives more freedom), are how they are handled by matlab and not in the syntax.

Values of Variables Matrix NumPy

I'm working on a program that determines if lines intersect. I'm using matrices to do this. I understand all the math concepts, but I'm new to Python and NumPy.
I want to add my slope variables and yint variables to a new matrix. They are all floats. I can't seem to figure out the correct format for entering them. Here's an example:
import numpy as np
x = 2
y = 5
w = 9
z = 12
I understand that if I were to just be entering the raw numbers, it would look something like this:
matr = np.matrix('2 5; 9 12')
My goal, though, is to enter the variable names instead of the ints.
You can do:
M = np.matrix([[x, y], [w, z]])
# or
A = np.array([[x, y], [w, z]])
I included the array as well because I would suggest using arrays instead of of matrices. Though matrices seem like a good idea at first (or at least they did for me), imo you'll avoid a lot of headache by using arrays. Here's a comparison of the two that will help you decide which is right for you.
The only disadvantage of arrays that I can think of is that matrix multiply operations are not as pretty:
# With an array the matrix multiply like this
matrix_product = array.dot(vector)
# With a matrix it look like this
matrix_product = matrix * vector
Can you just format the string like this?:
import numpy as np
x = 2
y = 5
w = 9
z = 12
matr = np.matrix('%s %s; %s %s' % (x, y, w, z))

Resources