I need to make an array A from the non-zero elements of an existing array B. In matlab I would "find" the indices of the nonzero values of B:
A=B(find(B~=0))
Could anyone suggest a way to do this in F90? In all of my tries, I always end up with A having the same length as B
Related
i want to use Eigen::sparseMatrix with double values and be able to retrieve all non zero elements of a specific column or row. Is it possible to do that without iterate over all elements ?
kind regards
Matt
I have a long, reasonably sparse boolean vector, that I want to iteratively select random elements from, and I was wondering what the most efficient way of doing so would be.
The vector can be up to around 100,000 elements long, and about 1 in every 20 elements will be "true" at any one time.
The selection of one of these elements, will occasionally result in making other elements available for selection; so I can't just do a single, initial pass of the boolean vector to get the indices of all the available elements and then shuffle that vector and pop elements, because the list of available elements changes.
I have worked out a couple of ideas, but can't really tell which would be best. So any insight would be greatly appreciated.
method 1:
given input boolean vector A
create boolean vector B // to store previously selected elements
create int vector C // to store currently available element indices
while stopping condition not met:
for each element a in A:
if a is "true":
append index of a to C
generate random integer i between 0 and length of A
set i-th element of C in A to "false"
set i-th element of C in B to "true"
compute any new "true" values of A
method 2:
given input boolean vector A
create boolean vector B // to store previously selected elements
create int vector C // to store currently available element indices
for each element a in A:
if a is "true":
append index of a to C
shuffle C
while stopping condition not met:
pop element from back of C
set i-th element of C in A to "false"
set i-th element of C in B to "true"
compute any new "true" values of A
if new values in A computed:
append index of new available element to C
shuffle C
Because not every selection from A results in a change to the set of available elements, I think method 2 will potentially be better than 1, except for the fact that I am not sure how much effort shuffling a long vector will cause.
method 3:
given input boolean vector A
create boolean vector B // to store previously selected elements
while stopping condition not met:
generate random integer i between 0 and length of A
If i is "true" in A:
set i in A to "false"
set i in B to "true"
compute any new "true" values of A
This final way seems a bit naive and simple, but I figured that if there will be about 1 in every 20 elements being true (except for the last group of elements, when no more can be added for ones that are selected), then on average it would only need about 20 tries for it to find a selectable element, which could actually be less effort than doing a full pass of the input vector, or shuffling the vector of available indices (especially if the vectors in question are quite long). Finding the last few would be very hard, but I could keep track of how many have been selected, and once the amount left gets below a certain level I could change how it is selected for the final lot.
Does anyone have any idea as to which might be more efficient? The implementation will be in C++ if that makes any difference.
Thanks for your help
You can change the representation of your sparse vector to the following -
Primary vector (the vector you have right now)
True vector (a list of all "true" indices)
Your operations now become -
Insert:
check if i in Primary Vector
if false, set to true and add to True Vector
Delete:
check if i in Primary Vector
if true, set to false and remove from True Vector by swapping
with last element and reducing size
(You will need pointers from Primary Vector to True Vector for this).
Random:
Generate random index j from size of (True Vector)
return True Vector[j]
All your operations can be done with O(1) complexity.
This sounds like a case for an Van Emde Boas tree
Space O(M)
Search O(log log M)
Insert O(log log M)
Delete O(log log M)
Annotate the aux array with number of members to make finding the random element easier.
I have this very large array, called grid. When I declare the array as below, every value in the array should be set to 0 according to the array constructor for integers
int testGrid[226][118];
However when I iterate through the entire array, I seem to get 0s for the majority of the array, however towards the lower part of the array I get arbitrary trash. The solution it is seems is to iterate over the array and manually set each value to 0. Is there a better way to do this?
You could do:
int testGrid[226][118] = {};
which will initialize your entries to 0.
Please see this C answer, which may come in handy for C++ too.
By the way, since this is C++, consider using an std::array, or an std::vector.
I know only the number of rows r in a matrix.
How do I read it into a multi-dimensional array arr[MAX][MAX]?
I thought of reading all the elements into a single array, count the no. of elements and then adding them to arr in groups of count/r. Is there a simpler way?
You could use the fact that everything may as well go into contiguous memory so just keep pushing it at the end of std::vector<double>. At the end you know its length, and given that you know r, you now also know the number of columns.
If you really have nothing but the number of rows and a list of data values, just read the whole thing into a vector, and then divide the size of the vector by the number of rows to get the number of columns. You should, however, also know whether the data is stored row-wise or column-wise. On this depends how to index the vector (I would keep the data in the vector and access it through index calculation, most probably encapsulated in a nice little class).
I am having a problem with what I think should be an easy piece of code. I have a 2D array that is N x M, currently stored in a boost multi_array. The N columns represent spatial dimensions e.g. x,y,z and the M rows are points along each dimension.
What I would like to do is print all possible combinations of points along each dimension
For example, if my array is:
-1 -1
1 1
I want to print:
-1 -1
1 -1
-1 1
1 1
I just cant make it work and I always go out of range on the array. I have tried using iterators and accessing the elements as A[i][j], but with no luck.
Anyone have any suggestions or thoughts?
You're likely not resetting the iterators by setting them back to begin(). A better strategy is not reusing the iterators at all. Make them local to the loop that you're using them in.