Replacing diagonal elements of square matrix without looping [duplicate] - arrays

This question already has answers here:
How to assign values to a MATLAB matrix on the diagonal?
(7 answers)
Closed 8 years ago.
I have a square matrix n x n & I also have a vector which is n x 1. I want to replace the diagonal elements with the values in my vector.
Is there a way of doing this in Matlab without looping?

matrix(1:n+1:end) = vector;
Explanation: if you use a single index into a matrix (that's called linear indexing), Matlab counts the elements down the first column, then second column etc. A step of n+1 thus defines the diagonal.

Related

How do I initialise an entire matrix with 0? [duplicate]

This question already has answers here:
Initializing variable length array [duplicate]
(3 answers)
Closed 2 years ago.
I want to initialise my matrix to 0, but it does not work because the first part of the matrix is a variable.
int playerCards[playerNum][10] = {0};
I want to make so all the values in the matrix are zero. I have tried the line above, but it tells me that i cannot initialise a the array. For reference, playerNum is an integer between 2 and 6, chosen by the user.
You can use memset():
int playerCards[playerNum][10];
memset(playerCards, 0, sizeof playerCards);
sizeof works for VLAs: in this case playerCards is evaluated, which does not make a difference, but also means that the size is computed at runtime as sizeof(int) * playerNum * 10 using the value of playerNum at the time of instantiation of playerCards.

Fortran operations on multidimensional arrays - sum example [duplicate]

This question already has answers here:
Sum of a two dimensional array
(1 answer)
sum only on certain dimension
(1 answer)
Closed 4 years ago.
I am trying to figure how to do simple algebraic operations on a subset of elements in a multidimensional array and assign the result to another array with the same dimensions.
program hello
IMPLICIT NONE
REAL,DIMENSION(10,10,2,1) :: tmp1=2
REAL,DIMENSION(10,10,2,1) :: tmp2=1
INTEGER,DIMENSION(2) :: myind=(/1,2/)
This operations returns only one value
Print *, sum(tmp1(:,:,itree,:))
While this returns the correct results, however I would expect the same result than the second operation, maybe I am thinking in the R way.
Print *, tmp1(:,:,1,:)+tmp1(:,:,2,:)
end program Hello
How do I achieve the result of the second operation and assign it to another multidimensional array? Imagine my index is large so I can't type out each single term in the operation.

Ruby: Compare if array contains other array [duplicate]

This question already has answers here:
How to determine if one array contains all elements of another array
(8 answers)
Closed 4 years ago.
how can i compare if an array of strings contains a smaller array of strings in Ruby?
e.g.
a=["1","2","3","4","5"]
b=["2","3"]
now i want to check if a contains b and get true/false
Thanks.
The most common approach would be to
(b - a).empty?
It has issues with unique elements, though. To detect whether a includes all elements from b, one should:
a_copy = a.dup
b.all? { |e| a_copy.delete e }
# or
b.all?(&a_copy.method(:delete))

Resizing a allocatable array [duplicate]

This question already has answers here:
Fortran increase dynamic array size in function
(3 answers)
Fortran array automatically growing when adding a value
(1 answer)
How to increase array size on-the-fly in Fortran?
(3 answers)
Closed 5 years ago.
I am trying to allocate an array and want to resize the array ie., grow the array size as and when required in the program.
My sample code looks like this
program main
implicit none
integer, allocatable, dimension(:)::test1
integer i, c1, c2
c1=10
c2=5
allocate(test1(1:c1))
! I basically want to do this
!allocate(test1(c1+1:c2))
end program main
How do I do this?

How to randomize numbers with no repeat in C? [duplicate]

This question already has answers here:
Unique (non-repeating) random numbers in O(1)?
(22 answers)
Unique random number generation in an integer array [duplicate]
(9 answers)
Closed 8 years ago.
I want to randomize number in each element of array in a variabel. I currently use srand() function. But, with this function i could get a same number in two or more element of array.
the output of my program is
number[0]=6
number[1]=3
number[2]=8
number[3]=3
See, number[1] and number[3] has same value. How to prevent this thing happen?

Resources