Related
I have a Matlab matrix M with size: [70 5 3 2 10 9 5 3 21];
I have a vector with a coordinates that I want to read of that matrix: [5, 1, 1, 2, 3, 4, 1, 2, 1];
MWE example of what I am trying to get:
M = rand(70 5 3 2 10 9 5 3 21);
coordinates = [5, 1, 1, 2, 3, 4, 1, 2, 1];
% Output desired:
M(5, 1, 1, 2, 3, 4, 1, 2, 1)
%Current attempt:
M(coordinates)
Clearly M(coordinates) <> M(5, 1, 1, 2, 3, 4, 1, 2, 1). Is there a way of doing this?
It's a bit awkward, but you can convert the array to a cell array, and then to a comma-separated list:
M = rand(70, 5, 3, 2, 10, 9, 5, 3, 21);
coordinates = [5, 1, 1, 2, 3, 4, 1, 2, 1];
coords_cell = num2cell(coordinates);
result = M(coords_cell{:});
I'm trying to make a sample lottery checker.
I'm using python.
x = [1,2,3,4,5,
y = [[1,2,3,4,5,6] # 6 numbers hit
,[1,2,3,4,6,7] # 5 numbers hit
,[2,3,4,6,7,8] # 4 numbers hit
,[4,5,6,7,8,9] # 3 numbers hit
,[1,2,7,8,9,10] # 2 numbers hit
,[4,7,8,9,10,11] # 1 number hit
,[7,8,9,10,11,12]]
output: (including the number of hits)
[1,2,3,4,5,6] 6 number hit
[1,2,3,4,6,7] 5 numbers hit
[2,3,4,6,7,8] 4 numbers hit
[4,5,6,7,8,9] 3 numbers hit
[1,2,7,8,9,10] 2 numbers hit
[4,7,8,9,10,11] 1 number hit
I tried using the any() function but only returned true or false.
please help.
Data:
x = [1,2,3,4,5,6]
y = [[1,2,3,4,5,6] # 6 numbers hit
,[1,2,3,4,6,7] # 5 numbers hit
,[2,3,4,6,7,8] # 4 numbers hit
,[4,5,6,7,8,9] # 3 numbers hit
,[1,2,7,8,9,10] # 2 numbers hit
,[4,7,8,9,10,11] # 1 number hit
,[7,8,9,10,11,12]]
Code:
for ticket in y:
print(ticket)
count = 0
for item in x:
if item in ticket:
count += 1
print(count, " numbers hit!")
Output:
[1, 2, 3, 4, 5, 6]
6 numbers hit!
[1, 2, 3, 4, 6, 7]
5 numbers hit!
[2, 3, 4, 6, 7, 8]
4 numbers hit!
[4, 5, 6, 7, 8, 9]
3 numbers hit!
[1, 2, 7, 8, 9, 10]
2 numbers hit!
[4, 7, 8, 9, 10, 11]
1 numbers hit!
[7, 8, 9, 10, 11, 12]
0 numbers hit!
I want to expand each element in a (2-by-2) matrix to a (3-by-2) block, using Python 3 --- with professional and elegant codes. Since I don't know the python codes, I will just describe the following in maths
X = # X is an 2-by-2 matrix.
1, 2
3, 4
d = (3,2) # d is the shape that each element in X should be expanded to.
Y = # Y is the result
1, 1, 2, 2
1, 1, 2, 2
1, 1, 2, 2
3, 3, 4, 4
3, 3, 4, 4
3, 3, 4, 4
Not that every element in X is now an 3-by-2 block in Y. The position of the block in Y is the same as the position of the element in X.
Here is the MATLAB code
X = [1,2;3,4];
d = [3,2]
[row, column] = size(X);
a = num2cell(X);
b = cell(row, column);
[b{:}] = deal(ones(d));
Y = cell2mat(cellfun(#times,a,b,'UniformOutput',false));
I appreciate your help. Thanks in advance.
If you are okay with using NumPy module with Python, you can use numpy.kron -
np.kron(X,np.ones((3,2),dtype=int))
Sample run -
In [15]: import numpy as np
In [16]: X = np.arange(4).reshape(2,2)+1 # Create input array
In [17]: X
Out[17]:
array([[1, 2],
[3, 4]])
In [18]: np.kron(X,np.ones((3,2),dtype=int))
Out[18]:
array([[1, 1, 2, 2],
[1, 1, 2, 2],
[1, 1, 2, 2],
[3, 3, 4, 4],
[3, 3, 4, 4],
[3, 3, 4, 4]])
In fact, this is a direct translation of how one would achieved the desired result in MATLAB in an elegant and professional way as well, as shown below -
>> X = [1,2;3 4]
X =
1 2
3 4
>> kron(X,ones(3,2))
ans =
1 1 2 2
1 1 2 2
1 1 2 2
3 3 4 4
3 3 4 4
3 3 4 4
Another way to do it with ndarray.repeat:
>>> X = np.arange(4).reshape(2,2)+1
>>> X.repeat(3, axis=0).repeat(2, axis=1)
array([[1, 1, 2, 2],
[1, 1, 2, 2],
[1, 1, 2, 2],
[3, 3, 4, 4],
[3, 3, 4, 4],
[3, 3, 4, 4]])
I have a data set consists of the following (which are head values for a finite difference groundwater flow model consists of 200 row, 200 column, and 5 layers)
, "id", "k", "i", "j", "f", "Active"
1, 1, 1, 1, 1, 313, 0
2, 2, 1, 1, 2, 315.2.0, 0
3, 3, 1, 1, 3, 301.24, 0
4, 4, 1, 1, 4, 306.05, 0
5, 5, 1, 1, 5, -999.0, 0
6, 6, 1, 1, 6, -999.0, 0
7, 7, 1, 1, 7, 310.57, 0
8, 8, 1, 1, 8, -999.0, 0
9, 9, 1, 1, 9, -999.0, 0
.
.
.
200000, 200000, 5, 200, 200, -999.0, 0
let us assume that I need to find the row that has a specific i,j,k
for example I want to find the row which has i=100, j=50, k=3 to store the value f for multiple i,j,k
I've tried to use find but it finds only the location for a specific item
I know it can be done using for & if but it will be time demanding
Is there a fast way to do so using matlab?
Lets suppose your text file has the following data
"id", "k", "i", "j", "f", "Active"
1, 1, 1, 1, 313, 0
2, 1, 1, 2, 315.2.0, 0
3, 1, 1, 3, 301.24, 0
4, 1, 1, 4, 306.05, 0
5, 1, 1, 5, -999.0, 0
6, 1, 1, 6, -999.0, 0
7, 1, 1, 7, 310.57, 0
8, 1, 1, 8, -999.0, 0
9, 1, 1, 9, -999.0, 0
First read the file through
>> fileID = fopen('testfileaccess.txt');
>> C = textscan(fileID,'%s %s %s %s %s %s','Delimiter',',')
You will get 6 cells representing each column
C =
Columns 1 through 5
{10x1 cell} {10x1 cell} {10x1 cell} {10x1 cell} {10x1 cell}
Column 6
{10x1 cell}
>> Matrix= [str2double(C{2}).';str2double(C{3}).';str2double(C{4}).';].'
the above code will result the i j and k in a matrix with each row representing each variable
Matrix =
NaN NaN NaN
1 1 1
1 1 2
1 1 3
1 1 4
1 1 5
1 1 6
1 1 7
1 1 8
1 1 9
then if you want to find for example k = 1 , i = 1 and j = 8 you use find()
find(Matrix(:,1) == 1 & Matrix(:,2) == 1 & Matrix(:,3) == 8)
and there you have it
ans =
8
8th row
row = jcount*kcount*(i-1) + kcount*(j-1) + k
In your case:
row = 200*5*(i-1) + 5*(j-1) + k
How to export any size matrix like
A=
1 2 3 4 5 6 ....9
3 6 7 8 9 9 ....4
...
6 7 7 4 4 5 ... 2
To a file that will contain that matrix where each value is separated by ',':
1, 2, 3, 4, 5, 6, ....,9
3, 6, 7, 8, 9, 9, ....,4
...
6, 7, 7, 4, 4, 5, ... ,2
Use DLMWRITE. Doing this:
>> A = [1 2 3 4; 5 6 7 8];
>> dlmwrite('file.csv', A);
writes a file with this:
1,2,3,4
5,6,7,8