How to convert vector in to an array? [closed] - octave

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to convert vector into an array in Ocatve.
I want to compare a string available in a vector or not. The vector is predefined. I am going to use strcmp function to check for the existance of a particular string in the list. This function works only with arrays, but not with vectors.
How can we convert a vector into an array in octave?
Please, find the difference between vector and arrays using strcmp in below screenshot:

Octave (and Matlab) do not have vector types. They also do not have list types.
What they do have are arrays, and cell arrays.
Both have 2 dimensions or more by convention.
A "horizontal vector" in octave is effectively a 2D array where the first dimension (i.e. rows) is 1.
A "vertical / column vector" in octave is effectively a 2D array where the second dimension (i.e. columns) is 1.
A "scalar" is a 2D array where the first and second dimensions are both 1.
Octave / matlab provides functions to test if an array is effectively a 'vector' or a 'scalar', but they are not considered as separate 'types'. So the question of converting a 'vector' to an equivalent 'array' does not make sense.
An array has the restriction that all its elements must be of the same type (e.g. numerical, character, class-derived objects, etc)
A cell array has no such restriction; each element of the cell array can have any type, including a numerical array or another cell array.
It sounds like what you want is to create a cell array, where you make each element a string.
You should be able to take it from there.

Related

Appropriate loss function in pytorch when output is an array of float numbers

I am writing an encoder/decoder model very similar to https://pytorch.org/tutorials/intermediate/seq2seq_translation_tutorial.html
The only difference is, here, the words are represented by some indices. I want to show them based on another metric, which are represented by flaot numbers.
The loss function nn.criterion = nn.NLLLoss(), seems to be working for times we are only workin with classes.
If my output array is not an array of integers, but an array of float numbers, what kind of loss function I can use? Considering all other parts are similar to the tutorial?
Thanks in advance.

Why did the designer make vector, map, and set functions in clojure?

Rich made vector, map, and set functions, while list, and sequence are not functions.
Why cannot all these collections be function to make it consistent?
Further, why don't we make all these compose data as a function which maps position to it's internal data?
If we make all these compose data as function then there will be only function and atom data in clojure. This will minimize the fundamental elements in that language right?
I believe a minimal, best only 2, set of fundamental elements would make the language simpler, more expressive and more flexible. Is this correct?
Vectors, maps, and sets are all associative data structures. Maps are the most obvious; they simply associate arbitrary keys with arbitrary values. A vector can be thought of as a map whose key set must be the set of all nonnegative integers less than the vector's size. Finally, sets can be thought of as maps that map keys to themselves.
It's important to understand that the sequential nature of a vector and the associative nature of a vector are two orthogonal things. It's a data structure that's designed to be good at supporting both abstractions (to some extent; for instance, you can't efficiently insert at the beginning of a vector).
Lists are simpler than vectors; they are finite sequential data structures, nothing more. A list can't efficiently return the element at a particular index, so it doesn't expose that functionality as part of its core interface. Of course, you can get an element of a list by index using nth, but in that case, you're explicitly treating it as a sequence, not as an associative structure.
So to answer your question, the IFn implementations for vectors, maps, and sets are there because of the extremely close relationship between the idea of an associative data structure and the idea of a pure function. Lists and other sequences are not inherently associative, so for consistency, they do not implement IFn.
Elogent's answer is excellent. There is one more reason that it wouldn't make sense for lists to be functions:
Literal lists already have a different, very important role, so they can't also be treated as functions in the way that vectors are.
Let's start with a vector containing two functions, partial and +, and a number, 5. We can treat the vector as a function, as you know, to return the value indexed by its argument:
user=> ([partial + 5] 2)
5
So far, so good. Suppose we want to use a list (partial + 5) in place of the vector, as you suggested, to return the value 5. Will we get an error message? No! But we won't get 5 as the result, either:
user=> ((partial + 5) 2)
7
What happened? (partial + 5) returned a function--the function that adds 5 to its single argument--and then this function was applied to the argument 2.
When a list is evaluated, its first element is evaluated, and should return a function. If the first element is a symbol, it's evaluated, and then the function that's its value is applied to the arguments, which are the other elements of the list. If the first argument of a list is itself a list, then it is evaluated in the same way that it would be evaluated if it were at the top level. The entire expression in that inner list should return a function, which will then be applied to the other elements of the outer list.
Since an inner list that's the first element of list that's being evaluated already has this role, it can't also play the kind of role that vectors that are first elements play.

Multiply two matrix in cuda c [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have to matrices A(32*32) and B(32*n), in which 'n' is coming from inputs and is between 2000 to 2000000.
I have two kind of inputs one is integers between 0 to 255 and the other one is 0,1. this multiplication is in a loop that iterates 3000 times. B(32*n) comes form input and is constant in all of the iterations but A(32*32) can change in each iteration.
//read B from file
//read A from file
double D[3000];
for(int i = 0; i < 3000; i++)
{
C = multiply(A, B);
// D[i] = mean of all elements in C
// build A from B using D[i] (this part is really complicated sequential process that contains lots of if and switches)
}
What is the fastest way to do this?
thank you.
Nobody here is going to write code for you, that is not what Stack Overflow is intended for. However, it would appear to be that there are a number of characteristics of the problem which you should be looking to exploit to improve the performance of your code:
Recognise that because one of the matrices only contains 0 or 1 and you are performing this in integer, what you are describing as matrix multiplication is really a large number of independent sparse sums
Recognise that because the next operation is to compute an average, you don't actually have to store the intermediate dot products and could directly perform a reduction on partial results of the matrix row summation
There are probably parallel primitives in the thrust library which you could use for prototyping, and an optimal hand written kernel would be aiming to fuse both the first and most of the second part of the operation into a single kernel.

Defining a Total Order [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How would you define a total order? For example, if you needed to define a total ordering or a shape, etc. How would you go about doing so?
Edit: Specifically, how would you define a total order based on an object with the coordinates (x,y,z). I don't understand how you could structure an ordering whereby each object is unique and sortable.
There is no "natural" ordering on 2D or 3D objects. However, if you want to induce an ordering, you can compare them by their coordinates, for example this way:
// returns -1 if o1<o2, 1 if o1>o2, 0 if o1==o2
int Compare(MyObject o1 ,MyObject o2)
{
if(o1.x>o2.x) return 1;
if(o1.x<o2.x) return -1;
if(o1.y>o2.y) return 1;
if(o1.y<o2.y) return -1;
if(o1.z>o2.z) return 1;
if(o1.z<o2.z) return -1;
return 0;
}
This assumes objects are uniquely identified by their coordinates, of course.
This ordering will let you sort and compare such objects. The question you have to answer yourself is if it helps you for any of the problems you want to solve with that. An ordering on a 1D-set is typically used to make lookups faster, especially when you want not only a specific element from your set, but all elements from a given range.
For 2D or 3D sets, a similar question is to find all element sets within a given rectangle or cube. For that purpose, the order above does not support you very well. There are datastructures like a 2D quadtree or 3D octree supporting this task much better.

Stream filter in cuda

I have an array of values and a linked list of indexes. Now, i only want to keep those values from the array that correspond to the indexes in the LL. is there a standard algorithm to do this. Please give example if possible
So, suppose i have an array 1,2,5,6,7,9
and i have a linked list 2->3
So, i want to keep the values at the index 2 and 3. That is keep 5 and 6.
Thus my function should return 5 and 6
In general, linked list is inherently serial. Having a parallel machine will not speed up the traversal of your list, hence the number of steps of your problem cannot go below O(n), where n is the size of the list.
However, if you have some additional way to access the list you can do something with it.
For example, all elements of the list could be stored in a fixed-size array (although, not necesairly in a consecutive way). List member could be represented in an array using the following struct.
struct ListNode {
bool isValid;
T data;
int next;
}
The value isValid sets if given cell in an array is occupied by a valid list member, or it is just an empty cell.
Now, a parallel algorithm would read all cells at once, check if it represents a valid data, and if so, do something with it.
Second part: Each thread, having a valid index idx of your input array A would have to mark A[idx] not to be deleted. Once we know which elements of A should be removed and which not - a parallel compaction algorithm can be applied.