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

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.

Related

Shifting array to left or right OCaml

I'm having some trouble around arrays in OCaml.
Namely, I want to just shift the elements to the right or left based on a value I pass.
Example: # let a = [|1;2;3;4;5|], # shift_array a 7;;- : int array array = [|4;5;1;2;3|]
I wrote the following code but I keep getting a syntax error where the second array is formed.
let shift_array arr x =
let size = Array.length arr in
let sec_arr = Array.make size 0 in
for i = 0 to size - 1 do
if i < x
then (sec_arr.(size - x + 1) <- arr.(i))
else (sec_arr.(i-x) <- arr.(i))
done;;
I'm just not 100% sure how to print out the new array.
EDIT: fixed it by adding in to the second and third line.
The problem now is that the function has type int array -> int -> unit and the one I'm trying to get is 'a array -> int -> 'a array. Is there some way to work around that ?
It should be let size = Array.length arr in, notice the in, which you're missing.
The let expression in OCaml has the form let <var> = <expr> in <body> and should not be (but commonly is) confused with the let definition that can occur only on the top-level (as an element of a module), which has form let <name> = <body>.
In your example, you have both, the top-level definition, let shift_array = <body> and two let expressions (though, you have used the wrong syntax for them.
EDIT:
Since OP edited the post, here is the corresponding edit.
You function doesn't return anything, it creates a new array, does the cycle, but doesn't return anything but the unit value (which is the value to which the for cycle evaluates). So you have to add one more line, that will contain the expression, to which the whole function will evaluate. Hint the sequencing operator ; is what you need, when you have expression x;y;z the computer evaluates x, then y, and finally z and the value of the whole expression of x;y;z is the value of z.

Matlab : Confusion over find() function

find() function returns the indices where the elements are non-zero. I tried with different array sizes but both give error :
In an assignment A(I) = B, the number of elements in B and I must be the same.
I am confused because when the array size is same, still I am getting this error.
This is just to understand what went wrong:
LEt,
Example 1: Same array size
A = [20;21;3;45;5;19;1;8;2;1];
B = A;
for i =1:length(B)
pos(i) = find(A == B(i));
end
I should have got pos = [1,2,3,4,5,6,7,8,9,10]. But the loops exits after i = 7, giving `pos = [1,2,3,4,5,6]'
Example 2: Dissimilar array size
C = [20;1;10;3];
for i =1:length(C)
pos(i) = find(A == C(i));
end
Can somebody please explain what is wrong in my understanding and an illustration of how I can work with same and different array length of A and B? Thank you.
The problem is that find(A == 1) returns two indexes, both 7 and 10, and that can't be stored in pos(i), since pos(i) can only hold a single number.
Unfortunately, the generic error message happened to have the same name for the matrices as two of your matrices, which can be confusing before you'we seen it a few times.

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.

C : explain theory behind

I have minimal experience with C, but have been asked to convert a C program into R. There is one bit troubling me:
I have a function that takes an int
int a
and within the function i have an array:
double b[3] = {
1.8293,
-0.592,
2.3330,
}
and later on in the function i have:
c = b[a];
can someone tell me what this line is doing? what is being assigned to c? is it simply an array of size b, where all the values of b have been multiplied by a?
Thanks.
c is assigned with the a th value of array b[].
if a is equal to 1 then c will be equal to -0.592
if a contains 0 then c = b[a] means that the first element of the array b[] is assigned to a, without changing array b
The variable a here is an index into the array. For example if a=0, then b[a] is the first element (1.8293 in this case).
In general b[a] extracts out a particular element from the array.
So here's a mock up of your function as you described it (I made up the parts you failed to mention) I added comments // to explain what is going on:
void foo(int a)
{
// define an array of 3 doubles
//b[0] = 1.8293
//b[1] = -0.592
//b[2] = 2.333
double b[3] = { 1.8293, -0.592, 2.3330 };
double c; // Note c is a double, because we're going to assign
// one of the values from b
//we're going to use a to access array b, that means a has to be 0, 1, or 2
if(a < 3)
c = b[a];
//Now c is set to one of those three values in b
//(depending on what value a has)
return;
}
The a'th element of the double array b is assigned to c (the first element has index 0).
you need to make some step in C learning.
b[a] means that you will get the content of element with index a from b array

MATLAB min(array) gives index exceeds array dimensions [duplicate]

This question already has an answer here:
sum(Array) says index exceeds matrix dimensions [duplicate]
(1 answer)
Closed 3 years ago.
I am trying to find the minimum value of a function of two variables, and then find the value of the variables.
My method is to iterate the function through several values of the variables and then use the min function to find the lowest value.
minval = -10;
maxval = 10;
n = 1;
for x = minval:maxval
for y = minval:maxval
f(n) = abs(x-1)+abs(y-1)+abs(x-3)+abs(y-5)+abs(x-8)+abs(y-3);
n=n+1;
end
end
f(n) = abs(x-1)+abs(y-1)+abs(x-3)+abs(y-5)+abs(x-8)+abs(y-3);
fmin = min(f)
The problem is with the last line:
fmin = min(f)
I am getting the error
??? Index exceeds matrix dimensions.
Error in ==> Lab2 at 65
fmin = min(f)
Why is this? Any help is greatly appreciated.
Don't define a variable named min. Try this:
which min
What does it tell you?
Note that functions in MATLAB can be overloaded by creating variables with the same name. When you do so, you prevent the function from being accessed by MATLAB. This is RARELY a good idea, so don't do it. The solution is
clear min
So you will delete that variable you have created. Of course, if there was something important in that variable, stuff it somewhere else first.
It does indeed look like you have declared a variable called min and so Matlab now treats it like a variable and not a function so it thinks you are trying to index the variable min with the vector f.
But just a comment on your code, leaving off whatever f(442) is you could achieve the same thing in a much more matlabesque fashion without loops like this:
minval = -10;
maxval = 10;
X = minval:maxval;
Y = X;
[xx, yy] = meshgrid(X, Y);
F = abs(xx-1) + abs(yy-1) + abs(xx-3) + abs(yy-5) +abs(xx-8) + abs(yy-3);
Your f is now equivalent to F(:)' (without the final value...), prove it to yourself like this: sum(f(1:end-1) == F(:)')
F as a matrix probably makes more sense than f as a flat vector anyway and you can find the min of F like this: min(F(:))
This code runs perfectly when I plug it into my version of Matlab.
If the error is occuring on line 65, then there must be something else going on in your program. Try and turn this part of your program into a function, so that it won't be impacted by everything else that you're working on.

Resources