I am going to convert an array as [1 2 6 12 13 15 20 8 30 31] to range [1 2 3 4 5 6 7 8 9 10] in EXCEL or in MATLAB. Is there any function to do it? Is there any suggestion to convert this type of arbitrary range to definite range?
Solution for MATLAB
In MATLAB, unique can do the trick. For example:
A = [1 1 1 1 4 4 5 3 3 6 6 6 6 8]
[C, ia, ic] = unique(A);
Now ic holds the values that you're looking for:
ic =
1 1 1 1 3 3 4 2 2 5 5 5 5 6
Also, you can remap ic to a new order if you wish to do so. For instance, here's a new order similar to the one you suggested:
new_order = [1 3 2 4 6 5];
ic_new = new_order(ic);
which yields:
ic_new =
1 1 1 1 2 2 4 3 3 6 6 6 6 5
Hope that helps!
In Matlab:
v = [1 2 6 12 13 15 20 8 30 31];
v_index = 1:length(v);
Related
I need to do a complicated vlookup/maxif type of selection. The data I have is as below
Row Col G Col H Col I colJ col K
1 Bench Strip Block BenchAbove BenchBelow
2 1 1 4
3 1 1 5
4 1 1 6
5 1 1 7
6 1 1 8
7 8 1 4 ?? ??
8 8 1 5
9 8 1 6
10 8 1 7
11 8 1 8
12 9 1 4
13 9 1 5
14 9 1 6
15 9 1 7
.....this list is long ( this is a sample only)
For every combination of (Strip, block) like say (1,4) There are benches like 1, 8 and 9. So bench above for 8 is 1 and bench below for 8 is 9. I need to determine the bench above and bench below for each row. There are no bench above 1 and no bench below 9.
I dont think vlookup is the solution here. Not sure if MAX(IF..) can help either. What would be the best formulae to obtain say on row 7, block combination is 1,4. The bench in question is 8. The bench above is 1 and bench below is 9. So 2 formulae will be required on Col J and Col I above.
The expected answer for the above sample data is :
Row Col G Col H Col I colJ col K
1 Bench Strip Block BenchAbove BenchBelow
2 1 1 4 - 8
3 1 1 5 - 8
4 1 1 6 - 8
5 1 1 7 - 8
6 1 1 8 - 8
7 8 1 4 1 9
8 8 1 5 1 9
9 8 1 6 1 9
10 8 1 7 1 9
11 8 1 8 1 9
12 9 1 4 8 -
13 9 1 5 8 -
14 9 1 6 8 -
15 9 1 7 8 -
Maybe in J2:
=IFERROR(LOOKUP(2,1/((H$1:H1=H2)*(I$1:I1=I2)),G$1:G1),"-")
In K2:
=IFERROR(INDEX(G3:G$16,MATCH(1,INDEX((H3:H$16=H2)*(I3:I$16=I2),),0)),"-")
However, I find your question a bit confusing so this answer might be a bit off.
If you have Office365 then you can use MAXIFS(), MINIFS() easily to get BenchAbove and BenchBelow. Try-
=MAXIFS(A2:A15,B2:B15,B7,C2:C15,C7)
=MINIFS(A2:A15,B2:B15,B7,C2:C15,C7)
EDIT: Solution for Excel-2016
Try below formula-
=INDEX($A$2:$A$15,AGGREGATE(14,6,ROW($A$2:$A$15)-ROW($A$1)/(($B$2:$B$15=B7)*($C$2:$C$15=C7)),ROW(1:1)))
=INDEX($A$2:$A$15,AGGREGATE(15,6,ROW($A$2:$A$15)-ROW($A$1)/(($B$2:$B$15=B7)*($C$2:$C$15=C7)),ROW(1:1)))
This question already has answers here:
Generate random array of floats between a range
(10 answers)
Closed 2 years ago.
Is it possible to generate random numbers in an nd-array such the elements in the array are between 1 and 2 (The interval should be between 1 and some number greater than 1 )? This is what I did.
input_array = np.random.rand(3,10,10)
But the values in the nd-array are between 0 and 1.
Please let me know if that is possible. Any help and suggestions will be highly appreciated.
You can try scaling:
min_val, max_val = 1, 2
input_array = np.random.rand(3,10,10) * (mal_val-min_val) + min_val
or use uniform:
input_array = np.random.uniform(min_val, max_val, (3,10,10))
You can use np.random.randInt() in order to generate nd array with random integers
import numpy as np
rand_arr=np.random.randint(low = 1, high = 10, size = (10,10))
print(rand_arr)
# It changes randomly
#Output:
[[6 9 3 4 9 2 6 2 9 7]
[7 1 7 1 6 2 4 1 8 6]
[9 5 8 3 5 9 9 7 8 4]
[7 3 6 9 9 4 7 2 8 5]
[7 7 7 4 6 6 6 7 2 5]
[3 3 8 5 8 3 4 5 4 3]
[7 8 9 3 5 8 3 5 7 9]
[3 9 7 1 3 6 3 1 4 6]
[2 9 3 9 3 6 8 2 4 8]
[6 3 9 4 9 5 5 6 3 7]]
I want to calculate the probability of all the data in a column dataframe according to its own distribution.For example,my data like this:
data
0 1
1 1
2 2
3 3
4 2
5 2
6 7
7 8
8 3
9 4
10 1
And the output I expect like this:
data pro
0 1 0.155015
1 1 0.155015
2 2 0.181213
3 3 0.157379
4 2 0.181213
5 2 0.181213
6 7 0.048717
7 8 0.044892
8 3 0.157379
9 4 0.106164
10 1 0.155015
I also refer to another question(How to compute the probability ...) and get an example of the above.My code is as follows:
import scipy.stats
samples = [1,1,2,3,2,2,7,8,3,4,1]
samples = pd.DataFrame(samples,columns=['data'])
print(samples)
kde = scipy.stats.gaussian_kde(samples['data'].tolist())
samples['pro'] = kde.pdf(samples['data'].tolist())
print(samples)
But what I can't stand is that if my column is too long, it makes the operation slow.Is there a better way to do it in pandas?Thanks in advance.
Its own distribution does not mean kde. You can use value_counts with normalize=True
df.assign(pro=df.data.map(df.data.value_counts(normalize=True)))
data pro
0 1 0.272727
1 1 0.272727
2 2 0.272727
3 3 0.181818
4 2 0.272727
5 2 0.272727
6 7 0.090909
7 8 0.090909
8 3 0.181818
9 4 0.090909
10 1 0.272727
I have a Cell Array of dimensions 2 by 40, and each cell contains a matrix of dimensions 3 by 3:
[3x3 double] [3x3 double] [3x3 double] ... [3x3 double]
[3x3 double] [3x3 double] [3x3 double] ... [3x3 double]
Now, I need to print this Cell Array and show it to my study group. Below is the detailed example of what I am trying to achieve.
Print should show each matrix in the cell clearly and separately, so that we can study it better, similar to this:
[1 2 3] [5 2 3]
[4 5 6] [1 2 2] ...
[7 8 9] [7 8 9]
[5 2 3] [4 5 6]
[3 5 6] [7 2 9] ...
[7 1 9] [5 2 3]
I tried converting Cell to Table, unfortunately this was untidy, as it showed each new row of matrices with semi-colons:
[1 2 3; 4 5 6; 7 8 9]
Also, I tried converting Cell to Double with:
data = [cell{1,:};cell{2,:}];
resulting in 6 by 120 double, looking like this:
1 2 3 5 2 3
4 5 6 1 2 2 ...
7 8 9 7 8 9
5 2 3 2 5 6
3 5 6 7 2 9 ...
7 1 9 5 2 3
I can export this to an Excel file, however you can see that it is still cumbersome. I can cut and paste the rows 4,5,6 and shift them below so that it looks like:
1 2 3 5 2 3
4 5 6 1 2 2 ...
7 8 9 7 8 9
5 2 3 2 5 6
3 5 6 7 2 9 ...
7 1 9 5 2 3
however I still need to divide every other 3 columns, so that I can achieve what I want, which is very impractical:
1 2 3 5 2 3
4 5 6 1 2 2 ...
7 8 9 7 8 9
5 2 3 2 5 6
3 5 6 7 2 9 ...
7 1 9 5 2 3
I would really appreciate your help on this one. Thank you in advance.
To display a complex cell array like this, you can use celldisp to display the cell in an understandable way.
% Create some example data
D = arrayfun(#(x)rand(3,3), ones(2,2), 'uni', 0);
% Display it
celldisp(D)
Another possible way to display this is to create a custom little routine to print the data.
formatspec = '%10.4f';
format = repmat([repmat([formatspec ' '], [1 size(D{1},2)]), ' '], [1 size(D,2)]);
arrayfun(#(k)fprintf('%s\n', sprintf([format, '\n'], cat(2, D{k,:}).')), 1:size(D,1))
A third option, if you really want to export to Excel, you will want to alter the fprintf statement to create a CSV file.
fid = fopen('data.csv', 'w');
% Create the format specifier for a row
format = repmat([repmat('%0.4f,', [1 size(D{1}, 2)]), ','], [1 size(D,2)]);
format(end-1:end) = '';
for k = 1:size(D, 1)
fprintf(fid, [format '\n'], cat(2, D{k,:}).');
% Create an empty row
fprintf(fid, ',\n');
end
fclose(fid);
The result when loaded in Excel.
I want to find the argmax of the values in a matrix by column, e.g.:
1 2 3 2 3 3
4 5 6 ->
3 7 8
I feel like I should just be able to map an argmax/posmax function over the columns, but I don't see a particularly intuitive way to do this in Octave.
Read max function documentation here
[max_values indices] = max(input);
Example:
input =
1 2 3
4 5 6
3 7 8
[max_values indices] = max(input)
max_values =
4 7 8
indices =
2 3 3
In Octave If
A =
1 3 2
6 5 4
7 9 8
1) For Each Column Max value and corresponding index of them can be found by
>> [max_values,indices] =max(A,[],1)
max_values =
7 9 8
indices =
3 3 3
2) For Each Row Max value and corresponding index of them can be found by
>> [max_values,indices] =max(A,[],2)
max_values =
3
6
9
indices =
2
1
2
Similarly For minimum value
>> [min_values,indices] =min(A,[],1)
min_values =
1 3 2
indices =
1 1 1
>> [min_values,indices] =min(A,[],2)
min_values =
1
4
7
indices =
1
3
1