Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

Generate a dataset

$
0
0

@STHossain wrote:

I am sorry maybe my title of the problem is probably not so appropriate, but here is the problem. So I have some arrays of categorical or boolean values and I want to create a combination of all of them and form an array. Also It could be the case that for each one of these combinations I have a unique value (in the example it is on the z vector). I tried with the following example and it worked. But honestly I didn’t like the way I could do this, is there a simpler way to do this?

Suppose I have

x = [false, true];
y = ['A', 'B'];
z = rand(4);

And I want to prodce a two dimensional array like following

4×3 Array{Any,2}:
 false  'A'  0.961038
  true  'A'  0.518147
 false  'B'  0.210022
  true  'B'  0.543537

Here is what I tried

A = [(i, j) for i in x, j in y];
B = [collect(zip(A[i]..., z[i])) for i in 1:length(A)];

C =  reshape([i for i in (B[1]...)], 1, 3);
for j in 2:length(B)
    C = vcat(C, reshape([i for i in (B[j]...)], 1, 3))
end
A

Posts: 5

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 2795

Trending Articles