How to plot each column of a cell array? - arrays

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

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))

View on Julia array using sliding window

What is the most efficient way to create a view on array using, for example, sliding window=2
Let's say we have:
x = collect(1:1:6)
# 1 2 3 4 5 6
And I want to create a view like this:
# 1 2
# 2 3
# 3 4
# 4 5
# 5 6
So far I found only this option, but not sure if it's an optimal one:
y = Array{Float32, 2}(undef, nslides, window)
#inbounds for i in 1:window
y[:, i] = #view x[i:end-(window-i)]
end
One solution with a package (well, with my package) is this:
julia> using Tullio
julia> x = 1:6; window = 2;
julia> #tullio y[r,c] := x[r+c-1] (c in 1:window)
5×2 Matrix{Int64}:
1 2
2 3
3 4
4 5
5 6
The one liner is:
view.(Ref(x), (:).(1:length(x)-1,2:length(x)))
Testing:
julia> x=collect(1:6);
julia> view.(Ref(x), (:).(1:length(x)-1,2:length(x)))
5-element Array{SubArray{Int64,1,Array{Int64,1},Tuple{UnitRange{Int64}},true},1}:
[1, 2]
[2, 3]
[3, 4]
[4, 5]
[5, 6]
Explanation:
creation of views is vectorized by the dot operator .
we do not want to vectorize on elements of x so use Ref(x) instead
(:) is just a shorter form for UnitRange and again we use the dot operator . to vectorize
I used 2 as the Window size but of course you can write view.(Ref(x), (:).(1:length(x)-(window-1),window:length(x)))
EDIT:
If you want rather a library function this would work for you:
julia> using ImageFiltering
julia> mapwindow(collect, x, 0:1,border=Inner())
5-element OffsetArray(::Array{Array{Int64,1},1}, 1:5) with eltype Array{Int64,1} with indices 1:5:
[1, 2]
[2, 3]
[3, 4]
[4, 5]
[5, 6]
Of course you could put them the function that you want to run on the sliding window rather than just collect.

Converting pandas data frame into numpy ndarray [duplicate]

This question already has answers here:
Convert pandas dataframe to NumPy array
(15 answers)
Closed 4 years ago.
I am using a pandas data frame to clean and process data. However, I need to then convert it into a numpy ndarray in order to use exploit matrix multiplication. I turn the data frame into a list of lists with the following:
x = df.tolist()
This returns the following structure:
[[1, 2], [3, 4], [5, 6], [7, 8] ...]
I then convert it into a numpy array like this:
x = np.array(x)
However, the following print:
print(type(x))
print(type(x[0]))
gives this result:
'numpy.ndarray'
'numpy.float64'
However, I need them both to be numpy arrays. If it's not from a pandas data frame and I just convert a hard-coded list of lists then they are both ndarrays. How do I get the list, and the lists in that list to be ndarrays when that list has been made from a data frame? Many thanks for reading, this has had me stumped for hours.
I think you need values:
df = pd.DataFrame({'C':[7,8,9,4,2,3],
'D':[1,3,5,7,1,0]})
print (df)
C D
0 7 1
1 8 3
2 9 5
3 4 7
4 2 1
5 3 0
x = df.values
print (x)
[[7 1]
[8 3]
[9 5]
[4 7]
[2 1]
[3 0]]
And then select by indexing:
print (x[:,0])
[7 8 9 4 2 3]
print (x[:,1])
[1 3 5 7 1 0]
print (type(x[:,0]))
<class 'numpy.ndarray'>
Also is possible transpose array:
x = df.values.T
print (x)
[[7 8 9 4 2 3]
[1 3 5 7 1 0]]
print (x[0])
[7 8 9 4 2 3]
print (x[1])
[1 3 5 7 1 0]
How about as_matrix:
x = df.as_matrix()
You may want to try df.get_values(), and eventually np.reshape it.

How do I input x,y,z co-ordinates as a single element inside a matrix?

I am trying to create a bezier surface using MATLAB code. For this I have to input co-ordinates in the form of [[x1 y1] [x2 y2] [x3 y3];[x4 y4] [x5 y5] [x6 y6]]. I have tried using cell array but arithmetical operations with other matrices or array isn't possible while using cell array. Please help
example:
C=[[2 3] [3 4] [4 5] [5 6];[2 5] [5 2] [7 8] [8 9]];
A=C(1,3);
ans=[4 5]
Also
C=[[2 3] [3 4] [4 5] [5 6];[2 5] [5 2] [7 8] [8 9]];
D=[1 2;2 1;3 1;2 3];
E=C*D
ans=[[30 38] [26 33];[49 51] [40 47]]
you can try using cat(3,..):
C = cat(3,[[2 3] ;[3 4] ;[4 5]; [5 6]],[[2 5]; [5 2] ;[7 8] ;[8 9]]);
A = C(3,:,1)
You could use a 3D matrix, with the second "layer" being your second coordinate pair, or simply use 2 matrices!
Using your example:
C1 = [2 3 4 5; 2 5 7 8];
C2 = [3 4 5 6; 5 2 8 9];
D = [1 2; 2 1; 3 1; 2 3];
E1 = C1*D; E2 = C2*D;
In 3D matrices:
% Make 3D matrix of same size as C1 but 2 layers
C = zeros([size(C1), 2]);
C(:,:,1) = C1; C(:,:,2) = C2;
E = cat(3, C(:,:,1)*D, C(:,:,2)*D);
% ans is a 3D matrix, with the 2 layers representing the pairs in your example.
Indexing the 3D matrix like you wanted:
C13 = reshape(C(1,3,:),1,2) % C13 = [4, 5]
% or
C13 = squeeze(C(1,3,:))' % C13 = [4, 5]

Slice array of arbitrary dimension with lists of start and end indices

I need to copy a part of a 3D array.
I have the indexes of start and end of the copy.
For example 2D array:
[[2 2 3 4 5]
[2 3 3 4 5]
[2 3 4 4 5]
[2 3 4 5 5]
[2 3 4 5 6]]
starting index, end index are:
mini = [2, 1]
maxi = [4, 3]
So the result should be:
[[3 4 4]
[3 4 5]]
I can write:
result = matrix[mini[0]:maxi[0], mini[1]:maxi[1]]
Is there a way to do it generally ? for 3Dim or NDim arrays ?
The trick here is realizing what the indexing syntax is under the hood. This:
result = matrix[mini[0]:maxi[0], mini[1]:maxi[1]]
Is shorthand in python (not just numpy) for:
indices = slice(mini[0], maxi[0]), slice(mini[1], maxi[1])
result = matrix[indices]
So we just need to generate indices dynamically:
lower = [2, 1, ...]
upper = [4, 3, ...]
indices = tuple(np.s_[l:u] for l, u in zip(lower, upper))
result = matrix_nd[indices]
np.s_[a:b] is a shorthand for slice(a, b). Here we build a tuple containing as many slices as you have values in lower and upper
What you are looking for is the slice object, see that example:
matrix = np.random.rand(4,5)
mini = [2, 1]
maxi = [4, 3]
slices=[slice(b,e) for b, e in zip(mini,maxi)]
print(slices)
print(matrix[slices])
print(matrix[mini[0]:maxi[0], mini[1]:maxi[1]])

Resources