I am trying to initialize the VHDL array of size 4 with all zeros. Please let me know how can I do that?
entity js is
port (
clk: in std_logic; ---------- clock
S1_vec : in t_1d_array; ---------- S1 vector/array of cross-section 4 [0 0 0 0]
S2_vec : in t_1d_array; ---------- S2 Vector/array of cross-section 4 [0 0 0 0]
J_outp : out integer
);
end js;
Related
I have the following dummy array:
>>> arr
[[0 0 1 0 0 1 0 1 1 1 0 0]
[1 1 0 1 1 0 0 0 0 0 1 1]
[1 1 0 1 0 1 0 0 0 0 1 0]
[1 1 0 1 1 0 1 1 0 0 0 1]
[0 1 0 1 0 1 1 1 0 0 0 1]
[1 0 1 1 1 0 0 1 0 0 1 1]]
I also have some "mutations" I would like to incorporate into my array, at the following positions (note these are generated randomly each time, so I cannot just set them manually):
row = [0 4 3]
col = [11 10 7]
I know I can target each (row, col) pair using fancy indexing with arr[row, col] = -3 (for example, set those elements to -3).
However, what I want to do is a bitwise NOT - aka something like this:
arr[row, col] = ~arr
I tried using np.where(), but it won't accept arr[row, col] b/c it doesn't generate a boolean array.
Any suggestions? How can I create a boolean array to use as a where conditional
(also yes, I know I can make an array of all zeros in the same shape as arr and then set those positions to 1's and use that as a mask - I'd love something cleaner tho)
Thanks!
I have written the following code to declare an array as data frame:
b=as.data.frame(array(0,dim=c(NF,29,1,T+1),
dimnames=list(NULL,c(…..varnames))))
Now, I am not able to move inside the array.. for instance, if I need to show all the matrices in the following array position [,,1,1], what I need to write?
I have tried code like:
b$[].1.1
b$,1.1
b[,,1,1]"
but, of course, it does not work.
Thank you very much for your help!
from ?as.data.frame :
Arrays can be converted to data frames. One-dimensional arrays are
treated like vectors and two-dimensional arrays like matrices. Arrays
with more than two dimensions are converted to matrices by
‘flattening’ all dimensions after the first and creating suitable
column labels.
array1 <- array(1:8,dim = c(2,2,2),dimnames = split(paste0(rep(letters[1:2],each=3),1:3),1:3))
# , , 3 = a3
#
# 2
# 1 a2 b2
# a1 1 3
# b1 2 4
#
# , , 3 = b3
#
# 2
# 1 a2 b2
# a1 5 7
# b1 6 8
#
df1 <- as.data.frame(array1)
# a2.a3 b2.a3 a2.b3 b2.b3
# a1 1 3 5 7
# b1 2 4 6 8
df1$b2.a3
# [1] 3 4
I need to create the a data frame, starting from an array which dimension is (2,3,1,3):
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
Hence, the output that I need is:
debt loan stock debt loan stock debt loan stock
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
Is next code correct?
b=array(0, dim=c(3,3,1,4), dimnames=list(NULL,c("debt","loan","stock")))
output=as.data.frame(b)
I have an array A of 1s and 0s and want to see if the larger array of bits B contains those bits in that exact order?
Example: A= [0 1 1 0 0 0 0 1]
B= [0 1 0 0 1 1 0 0 0 0 1 0 1 0 1]
would be true as A is contained in B
Most solutions I have found only determine if a value IS contained in another matrix, this is no good here as it is already certain that both matrices will be 1s and 0s
Thanks
One (albeit unusual) option, since you're dealing with integer values, is to convert A and B to character arrays and use the contains function:
isWithin = contains(char(B), char(A));
There are some obtuse vectorized ways to to do this, but by far the easiest, and likely just as efficient, is to use a loop with a sliding window,
A = [0 1 1 0 0 0 0 1];
B = [0 1 0 0 1 1 0 0 0 0 1 0 1 0 1];
vec = 0:(numel(A)-1);
for idx = 1:(numel(B)-numel(A)-1)
if all(A==B(idx+vec))
fprintf('A is contained in B\n');
break; % exit the loop as soon as 1 match is found
end
end
Or if you want to know the location(s) in B (of potentially multiple matches) then,
A = [0 1 1 0 0 0 0 1];
B = [0 1 0 0 1 1 0 0 0 0 1 0 1 0 1];
C = false(1,numel(B)-numel(A)-1);
vec = 0:(numel(A)-1);
for idx = 1:numel(C)
C(idx) = all(A==B(idx+vec));
end
if any(C)
fprintf('A is contained in B\n');
end
In this case
>> C
C =
1×6 logical array
0 0 0 1 0 0
You can use the cross-correlation between two signals for this, as a measure of local similarity.
For achieving good results, you need to shift A and B so that you don't have the value 0 any more. Then compute the correlation between the two of them with conv (keeping in mind that the convolution is the cross-correlation with one signal flipped), and normalize with the energy of A so that you get a perfect match whenever you get the value 1:
conv(B-0.5, flip(A)-0.5, 'valid')/sum((A-0.5).^2)
In the normalization term, flipping is removed as it does not change the value.
It gives:
[0 -0.5 0.25 1 0 0 -0.25 0]
4th element is 1, so starting from index equal to 4 you get a perfect match.
I have a 3D array of dimensions (200,200,3). These are images of dimensions (200,200) stacked using numpy.dstack. I would like to count the number of values along axis=2 that are greater than a corresponding 2D threshold array of dimensions (200,200). The output counts array should have dimensions (200,200). Here is my code so far.
import numpy as np
stacked_images=np.random.rand(200,200,3)
threshold=np.random.rand(200,200)
counts=(stacked_images<threshold).sum(axis=2)
I am getting the following error.
ValueError: operands could not be broadcast together with shapes (200,200,3) (200,200)
The code works if threshold is an integer/float value. For example.
threshold=0.3
counts=(stacked_images<threshold).sum(axis=2)
Is there a simple way to do this if threshold is a 2D array? I guess I am not understanding numpy broadcasting rules correctly.
numpy is expecting to make a value by value operation. In your case you seem to be wanting to know if any value in the full Z (axis=2) trace exceeds the equivalent x, y value in threshold.
As so just make sure threshold has the same shape, namely by building a 3D threshold using whatever method you prefer. Since you mentioned numpy.dstack:
import numpy as np
stacked_images = np.random.rand(10, 10, 3)
t = np.random.rand(10, 10)
threshold = np.dstack([t, t, t])
counts = (stacked_images < threshold).sum(axis=2)
print(counts)
, which results in:
[[2 0 3 3 1 3 1 0 1 2]
[0 1 2 0 0 1 0 0 1 3]
[2 1 3 0 3 2 1 3 1 3]
[2 0 0 3 3 2 0 2 0 1]
[1 3 0 0 0 3 0 2 1 2]
[1 1 3 2 3 0 0 3 0 3]
[3 1 0 1 2 0 3 0 0 0]
[3 1 2 1 3 0 3 2 0 2]
[3 1 1 2 0 0 1 0 1 0]
[0 2 2 0 3 0 0 2 3 1]]
I recently picked up the Go language, and now I am confused with the following code:
package main
import "fmt"
func main() {
a := make([]int, 5)
printSlice("a", a)
b := make([]int, 0, 5)
printSlice("b", b)
c := b[:2]
printSlice("c", c)
d := c[2:5]
printSlice("d", d)
}
func printSlice(s string, x []int) {
fmt.Printf("%s len=%d cap=%d %v\n",
s, len(x), cap(x), x)
}
And the result:
a len=5 cap=5 [0 0 0 0 0]
b len=0 cap=5 []
c len=2 cap=5 [0 0] //why the capacity of c not 2 but 5 instead
d len=3 cap=3 [0 0 0]
c is a slice taken from the array b. This isn't a copy, but just a window over the 2 first elements of b.
As b has a capacity of 5, c could be extended to take the 3 other places (in fact it makes a new slice but over the same place in memory).
The maximal capacity of the slice is the capacity of the underlying array minus the position of the start of the slice in the array :
array : [0 0 0 0 0 0 0 0 0 0 0 0]
array : <---- capacity --->
slice : [0 0 0 0]
slice : <---- capacity --->
Maybe this program will make it more clear that c and d are just windows over b :
func main() {
b := make([]int, 0, 5)
c := b[:2]
d := c[1:5] // this is equivalent to d := b[1:5]
d[0] = 1
printSlice("c", c)
printSlice("d", d)
}
Output :
c len=2 cap=5 [0 1] // modifying d has modified c
d len=4 cap=4 [1 0 0 0]
Note that in go 1.2 (Q4 2013, 1.2rc1 is available now), you can associate to a slice a capacity of its own (instead of a capacity deduced from the underlying array).
See "Three-index slices", and the design document.
A slicing operation creates a new slice by describing a contiguous section of an already-created array or slice:
var array [10]int
slice := array[2:4]
The capacity of the slice is the maximum number of elements that the slice may hold, even after reslicing; it reflects the size of the underlying array.
In this example, the capacity of the slice variable is 8.
(capacity of the underlying array minus the position of the start of the slice in the array)
array : [0 0 0 0 0 0 0 0 0 0]
array : <---- capacity --->
slice : [0 0]
slice : <-- capacity --> 8 (10-2)
Go 1.2 adds new syntax to allow a slicing operation to specify the capacity as well as the length.
A second colon introduces the capacity value, which must be less than or equal to the capacity of the source slice or array, adjusted for the origin.
For instance,
slice = array[2:4:6]
array : [0 0 0 0 0 0 0 0 0 0]
array : <---- capacity ---> 10
slice : [0 0]
slice : <- cap-> 4 (6-2)
sets the slice to have the same length as in the earlier example but its capacity is now only 4 elements (6-2).
It is impossible to use this new slice value to access the last two elements of the original array.
The main argument is to give programmers more control over append.
a[i : j : k]
That slice has:
indices starting at 0
length equals to j - i
capacity equals to k - i
The evaluation panics if i <= j <= k <= cap(a) is not true.