How to convert a cell array of struct to a cell array - arrays

I have a cell array of such:
data{1}.field1 = [1 2 3 4 5 6];
data{2}.field1 = [7 8 9 10];
...
data{1}.field2 = {[1 2 3]; [4 5 6]};
data{2}.field2 = {[7 8 9]};
...
I there a simple trick to convert that so I have:
field1 = {[1 2 3 4 5 6], [7 8 9 10], ...}
field2 = {{[1 2 3];[4 5 6]}, {[7 8 9]}, ...}

Ok I've found a solution that seem to work
tmp = [data{:}];
field1 = {tmp.field1};
field2 = {tmp.field2};`

Related

How can I add 1 in the beginning of a numpy array?

I have a numpy array X = [[3 4 5 6] [6 5 3 3] [9 8 5 2]]
I would like to add 1 in each array like so:
X = [[1 3 4 5 6] [1 6 5 3 3] [1 9 8 5 2]]
I wanted to do it using np.ones() and np.hstack()
This is what I tried to do
X = [[3 4 5 6] [6 5 3 3] [9 8 5 2]]
ones = np.ones(len(X))
X = np.hstack((ones, X))

Slice matrix every 2 columns

I am trying to figure out how to slice a matrix every 2 columns. For example, if we have
A=[[1 2 3 4 5 6];[7 8 9 10 11 12]]
2×6 Matrix{Int64}:
1 2 3 4 5 6
7 8 9 10 11 12
then I would like to return 3 matrices sliced every 2 columns, i.e. a 3 dimensional array with 3 matrices,
2×2×3 Array{Int64, 3}:
[:, :, 1] =
1 2
7 8
[:, :, 2] =
3 4
9 10
[:, :, 3] =
5 6
11 12
One thing I have tried is the eachslice function,
collect(eachslice(A,dims=2))
6-element Vector{SubArray{Int64, 1, Matrix{Int64}, Tuple{Base.Slice{Base.OneTo{Int64}}, Int64}, true}}:
[1, 7]
[2, 8]
[3, 9]
[4, 10]
[5, 11]
[6, 12]
but this slices along every column, but I want to slice along every two columns!
Thanks in advance.
That's just a reshape. Reshapes share memory (like views), so you should copy it if you want it to be independent of the original A:
julia> reshape(A, 2, 2, 3)
2×2×3 Array{Int64, 3}:
[:, :, 1] =
1 2
7 8
[:, :, 2] =
3 4
9 10
[:, :, 3] =
5 6
11 12
But if you really want, you can express it as indexing, too:
julia> A[:, [1:2 3:4 5:6]]
2×2×3 Array{Int64, 3}:
[:, :, 1] =
1 2
7 8
[:, :, 2] =
3 4
9 10
[:, :, 3] =
5 6
11 12

how to convert from Array{Array{Int64,2},1} to Array{Int64,2}

In Julia, I want to convert data defined as Vector of 2D array to a 2D array of Matrix.
As described in the following example, I want to convert data s into data t, but I have not been successful so far.
How should I deal with the case?
julia> s = [[1 2 3], [4 5 6], [7 8 9]]
3-element Array{Array{Int64,2},1}:
[1 2 3]
[4 5 6]
[7 8 9]
julia> t = [[1 2 3]; [4 5 6]; [7 8 9]]
3××3 Array{Int64,2}:
1 2 3
4 5 6
7 8 9
julia> s |> typeof
Array{Array{Int64,2},1}
julia> t |> typeof
Array{Int64,2}
julia> convert(Array{Int64, 2}, s)
ERROR: MethodError: Cannot `convert` an object of type Array{Array{Int64,2},1} to an object of type Array{Int64,2}
This may have arisen from a call to the constructor Array{Int64,2}(...),
since type constructors fall back to convert methods.
julia> reshape(s, 3, 3)
ERROR: DimensionMismatch("new dimensions (3,3) must be consistent with array size 3")
in reshape(::Array{Array{Int64,2},1}, ::Tuple{Int64,Int64}) at .\array.jl:113
in reshape(::Array{Array{Int64,2},1}, ::Int64, ::Int64, ::Vararg{Int64,N}) at .\reshapedarray.jl:39
As in the following example, if you define 2D array or 1D array as source data, I can then reshape them successfully into a 2D array of Matrix.
julia> u = [1 2 3 4 5 6 7 8 9]
1××9 Array{Int64,2}:
1 2 3 4 5 6 7 8 9
julia> u |> typeof
Array{Int64,2}
julia> reshape(u, 3, 3)
3××3 Array{Int64,2}:
1 4 7
2 5 8
3 6 9
julia> v = [1, 2, 3, 4, 5, 6, 7, 8, 9]
9-element Array{Int64,1}:
1
2
3
4
5
6
7
8
9
julia> v |> typeof
Array{Int64,1}
julia> reshape(v, 3, 3)
3××3 Array{Int64,2}:
1 4 7
2 5 8
3 6 9
You can use vcat and splatting
julia> t = vcat(s...)
3×3 Array{Int64,2}:
1 2 3
4 5 6
7 8 9

How to plot each column of a cell array?

I have a cell array that looks like this:
Column1 Column2
[1 2 3 4] [2 5 6 9]
[1 3 4] [3 4 7 8]
[2 3 4] [1 3 7 9]
[1 2 4] [1 4 6 8]
There are a few more columns that have similar styles of data. I need to create a way to make a graph of each column (separate graphs for each column of the array), that plots each point as a number from each double as the x-coordinate, and the row as the y-coordinate. It should look something like this:
(Row)
1 x x x x
2 x x x
3 x x x
4 x x x
1 2 3 4
X is just a point on the graph.
Does this make enough sense? I feel like I'm making 0 progress in explaining what I want. If anyone doesn't understand this, feel free to ask questions and I'll answer them as best I can.
Something like this?
cin = { {[1 2 3 4] , [1 3 4], [2 3 4], [1 2 4]}, {[1 2 3 8] , [1 3 4], [2 3 4], [1 2 4]} };
for k=1:numel(cin)
col_k = cin{k};
figure(); %// 1 figure per column
for y=1:numel(col_k)
plot(col_k{y}, y);
hold on;
end
end

Place equal elements in cell array

I have an array. I sorted it, so I have sorted array and indeces of sorted elements in the initial array.
Fo example, from [4 5 4 4 4 4 5 4] I got [4 4 4 4 4 4 5 5] and [1 3 4 5 6 8 2 7].
How to place recieved indeces in a cell array, so that in one cell will be indeces of equal elements? For my example, it will be: {1 3 4 5 6 8}, {2 7}.
I'm searching for non-loop way to solve it.
Use accumarray:
x = [4 5 4 4 4 4 5 4]; %// data
[~, ~, jj] = unique(x);
result = accumarray(jj(:), 1:numel(x), [], #(v) {v(:).'});
Or, if you need each set of indices sorted:
result = accumarray(jj(:), 1:numel(x), [], #(v) {sort(v(:)).'});

Resources