variable-assignment

How can I assign a value to the diagonals of a 4-D matrix using linear indexing in MATLAB?

前提是你 提交于 2019-12-01 19:27:46
I have a 4-D matrix A of size NxNxPxQ . How can I easily change the diagonal values to 1 for each NxN 2-D submatrix in a vectorized way? Incorporating gnovice's suggestion, an easy way to index the elements is: [N,~,P,Q]=size(A);%# get dimensions of your matrix diagIndex=repmat(logical(eye(N)),[1 1 P Q]);%# get logical indices of the diagonals A(diagIndex)=1;%# now index your matrix and set the diagonals to 1. You can actually do this very simply by directly computing the linear indices for every diagonal element, then setting them to 1: [N,N,P,Q] = size(A); diagIndex = cumsum([1:(N+1):N^2; N

Assignment issue OCJP; why can't I pass an int to a short?

好久不见. 提交于 2019-12-01 19:01:39
I have two pieces of code. One works, another doesn't, but both seem to do identical things. This works: short s=7; but the below code doesn't. Instead, it gives error: can't assign int to short I know an integer number literal by default is int , but if it can be assigned directly above, then why not when passing to a method? class Demo1{ public static void main(String[] args){ new Demo1().go(7); } void go(short s){System.out.println("short");} } The rules are different for assignment and for method overload resolution : For assignment the JLS says : In addition, if the expression is a

matlab conditioned matrix assignment

为君一笑 提交于 2019-12-01 18:57:56
i have a question about matrix assignment. say i have three matrices A, B and C, and i want to assign the elements of matrix C to the elements of A and B according to the rule C[i,j] = A[i,j] if abs(C[i,j] - A[i,j]) < abs(C[i,j] - B[i,j]) C[i,j] = B[i,j] if abs(C[i,j] - A[i,j]) > abs(C[i,j] - B[i,j]) C[i,j] = 0 if abs(C[i,j] - A[i,j]) == abs(C[i,j] - B[i,j]) how can i write it without for loops? thanks very much for your help. I think Dan Becker has the right idea, but re-computing abs(C-B) and abs(C-A) implies that the updated matrices are compared, not the original ones. I don't think this

struct initialization / assignment with braces

时光毁灭记忆、已成空白 提交于 2019-12-01 18:36:28
问题 I have defined a struct as follows: struct float3 { float x; float y; float z; float3 () : x(0), y(0), z(0) {} float3 (float a, float b, float c) : x(a), y(b), z(c) {} }; But i have trouble when it comes to understanding the different ways of initializing / assigning values to its members. For instance: //Initialization float3 3Dvec = {1.0, 1.0, 1.0}; float3 3Dvec2 {1.0, 1.0, 1.0}; float3 3Dvec3 (1.0, 1.0, 1.0); //Assignment 3Dvec = {2.0, 2.0, 2.0}; 3Dvec = float3 (2.0, 2.0, 2.0); All of

How does multiple assignment work?

那年仲夏 提交于 2019-12-01 18:08:31
问题 From Section 4.1 of Programming in Lua. In a multiple assignment, Lua first evaluates all values and only then executes the assignments. Therefore, we can use a multiple assignment to swap two values, as in x, y = y, x -- swap x' for y' How does the assignment work actually? 回答1: How multiple assignment gets implemented depends on what implementation of Lua you are using. The implementation is free to do things anyway it likes as long as it preserves the semantics. That is, no matter how

Python: What does the use of [] mean here?

走远了吗. 提交于 2019-12-01 17:58:19
What is the difference in these two statements in python? var = foo.bar and var = [foo.bar] I think it is making var into a list containing foo.bar but I am unsure. Also if this is the behavior and foo.bar is already a list what do you get in each case? For example: if foo.bar = [1, 2] would I get this? var = foo.bar #[1, 2] and var = [foo.bar] #[[1,2]] where [1,2] is the first element in a multidimensional list [] is an empty list. [foo.bar] is creating a new list ( [] ) with foo.bar as the first item in the list, which can then be referenced by its index: var = [foo.bar] var[0] == foo.bar #

Basic Numpy array value assignment

偶尔善良 提交于 2019-12-01 17:54:32
As a small exercise before i start playing with numeric code in python I am trying to make an LDLT algorithm. Just to "get the feet wet". However I seem to be lacking a fundamental understanding of the numpy array. See the following example: def ldlt(Matrix): import numpy (NRow, NCol) = Matrix.shape for col in range(NCol): Tmp = 1/Matrix[col,col] for D in range(col+1, NCol): Matrix[col,D] = Matrix[D,col]*Tmp if __name__ == '__main__': import numpy A = numpy.array([[2,-1,0],[-1,2,-1],[0,-1,2]]) ldlt(A) The example is not the full code I am working on. However, try and run it, and set a break

Basic Numpy array value assignment

谁都会走 提交于 2019-12-01 17:39:31
问题 As a small exercise before i start playing with numeric code in python I am trying to make an LDLT algorithm. Just to "get the feet wet". However I seem to be lacking a fundamental understanding of the numpy array. See the following example: def ldlt(Matrix): import numpy (NRow, NCol) = Matrix.shape for col in range(NCol): Tmp = 1/Matrix[col,col] for D in range(col+1, NCol): Matrix[col,D] = Matrix[D,col]*Tmp if __name__ == '__main__': import numpy A = numpy.array([[2,-1,0],[-1,2,-1],[0,-1,2]]

Why can't I directly assign an int to an int pointer like this: int *p = 6;?

北城余情 提交于 2019-12-01 16:57:30
error: invalid conversion from 'int' to 'int*' int *q = 8; Works fine. *q = 6; Why can't I directly assign an int to an int pointer like this: int *q = 6 ; and I can assign it safely in the next line? Because they're different things at all. The 1st one is definition of variable with initializer expression, i.e an initialization (of the pointer itself): int * q = 8; ~~~~~ ~ ~~~ type is int*; name of variable is q; initialized with 8 The 2nd one is assignment (of the object pointed by the pointer): *q = 6; ~~ ~~~ dereference on q via operator*; assign the resulting lvalue pointed by q to 6 And,

Why does single `=` work in `if` statement?

可紊 提交于 2019-12-01 16:31:32
This code is provided as an example in for use with devise and OmniAuth, it works in my project . class User < ActiveRecord::Base def self.new_with_session(params, session) super.tap do |user| if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"] user.email = data["email"] if user.email.blank? end end end end I don't know why it's a single equals sign as apposed to a double equals sign, which I thought was necessary for if -statements. My IDE "intelliJ IDEA" agrees with my concerns. The only necessary thing for an if statement to be valid is a