variable-assignment

What exactly are C++ definitions, declarations and assignments?

◇◆丶佛笑我妖孽 提交于 2019-11-26 17:47:23
问题 I tend to use the words define, declare and assign interchangeably but this seems to cause offense to some people. Is this justified? Should I only use the word declare for the first time I assign to a variable? Or is there more to it than that? 回答1: Define and declare are similar but assign is very different. Here I am declaring (or defining) a variable: int x; Here I am assigning a value to that variable: x = 0; Here I am doing both in one statement: int x = 0; Note Not all languages

How to assign a value to a TensorFlow variable?

旧街凉风 提交于 2019-11-26 17:25:29
I am trying to assign a new value to a tensorflow variable in python. import tensorflow as tf import numpy as np x = tf.Variable(0) init = tf.initialize_all_variables() sess = tf.InteractiveSession() sess.run(init) print(x.eval()) x.assign(1) print(x.eval()) But the output I get is 0 0 So the value has not changed. What am I missing? The statement x.assign(1) does not actually assign the value 1 to x , but rather creates a tf.Operation that you have to explicitly run to update the variable.* A call to Operation.run() or Session.run() can be used to run the operation: assign_op = x.assign(1)

Golang mixed assignment and declaration

折月煮酒 提交于 2019-11-26 16:59:31
问题 I started working with go for a few weeks, and (once again) I stumbled across something that seems odd for me: // Not working a := 1 { a, b := 2, 3 } // Works a := 1 a, b := 2, 3 playground I want to assign two variables simultaneously. One is already declared, in a superior scope, the other one is not. It does not work: the compiler tries to redeclare the former variable. However, it works fine if this variable is declared in the same scope. Why is that? 回答1: What you're experiencing is

Assign names to vector entries without assigning the vector a variable name?

天涯浪子 提交于 2019-11-26 16:49:23
问题 In R, is it possible to assign names to components of a vector without first assigning that vector to a variable name? The normal way is obviously: z <- 1:3 names(z) <- c("a", "b", "c") #normal way names(1:3) <- c("a", "b", "c") #throws an error The second way throws "Error in names(1:3) <- c("a", "b", "c") : target of assignment expands to non-language object" According to the doc, the expression is evaluated as z <- "names<-"(z, "[<-"(names(z), 3, "c2"))’. So no shock it doesn't work, I'm

How can I change the values of multiple points in a matrix?

感情迁移 提交于 2019-11-26 16:48:39
I have a matrix that is [500x500]. I have another matrix that is [2x100] that contains coordinate pairs that could be inside the first matrix. I would like to be able to change all the values of the first matrix to zero, without a loop. mtx = magic(500); co_ords = [30,50,70; 30,50,70]; mtx(co_ords) = 0; gnovice You can do this using the function SUB2IND to convert your pairs of subscripts into a linear index: mtx(sub2ind(size(mtx),co_ords(1,:),co_ords(2,:))) = 0; shangping Another answer: mtx(co_ords(1,:)+(co_ords(2,:)-1)*500)=0; I've stumbled upon this question while I was looking for a

Why does C++ allow an integer to be assigned to a string?

淺唱寂寞╮ 提交于 2019-11-26 16:37:42
I encountered an interesting situation today in a program where I inadvertantly assigned an unsigned integer to a std::string. The VisualStudio C++ compiler did not give any warnings or errors about it, but I happened to notice the bug when I ran the project and it gave me junk characters for my string. This is kind of what the code looked like: std::string my_string(""); unsigned int my_number = 1234; my_string = my_number; The following code also compiles fine: std::string my_string(""); unsigned int my_number = 1234; my_string.operator=(my_number); The following results in an error:

Setting one object equal to another object with the assignment operator in Javascript

我的梦境 提交于 2019-11-26 16:36:21
问题 I'm coming to javascript from C background. In javascript, when I use the assignment operator to assign one object to another, does it copy the values from one to the another, or do they both now point to the same data?. Or does the assignment operator do anything in this case? function point_type() { this.x = 0; this.y = 0; } var pnt1 = new point_type(); var pnt2 = new point_type(); pnt1.x = 4; pnt1.y = 5; pnt2 = pnt1; pnt1.x = 8; pnt2.y = 9; In the example above, does pnt2.x now equal 8, or

Multiple assignment semantics

我只是一个虾纸丫 提交于 2019-11-26 16:14:23
In Python one can do: a, b = 1, 2 (a, b) = 1, 2 [a, b] = 1, 2 I checked the generated bytecode using dis and they are identical. So why allow this at all? Would I ever need one of these instead of the others? One case when you need to include more structure on the left hand side of the assignment is when you're asking Python unpack a slightly more complicated sequence. E.g.: # Works >>> a, (b, c) = [1, [2, 3]] # Does not work >>> a, b, c = [1, [2, 3]] Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: need more than 2 values to unpack This has proved useful for

What happens when I assign long int to int in C?

最后都变了- 提交于 2019-11-26 15:51:14
问题 In a recent homework assignment I've been told to use long variable to store a result, since it may be a big number. I decided to check will it really matter for me, on my system (intel core i5/64-bit windows 7/gnu gcc compiler) and found out that the following code: printf("sizeof(char) => %d\n", sizeof(char)); printf("sizeof(short) => %d\n", sizeof(short)); printf("sizeof(short int) => %d\n", sizeof(short int)); printf("sizeof(int) => %d\n", sizeof(int)); printf("sizeof(long) => %d\n",

Is there anything like deal() for normal MATLAB arrays? [duplicate]

人盡茶涼 提交于 2019-11-26 14:49:16
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: How do I do multiple assignment in MATLAB? When dealing with cell arrays, I can use the deal() function to assign cells to output variables, such as: [a, b, c] = deal(myCell{:}); or just: [a, b, c] = myCell{:}; I would like to do the same thing for a simple array, such as: myArray = [1, 2, 3]; [a, b, c] = deal(myArray(:)); But this doesn't work. What's the alternative? 回答1: One option is to convert your array to