Is there a way that an Octave Matrix would hold Strings and numbers together?
I want to have a matrix of the fallowing type:
A=["A","B","C","D";1,2,3,4;2,3,4,5;3,4,5,6;4,5,6,7];
So that the matrix will look like:
A B C D
1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7
But when I try this I get:
ABCD
empty line
empty line
empty line
empty line
*empty line represents an empty line
And if I try to put strings that are more than 1 character in length, I get a number of columns mismatch error.
Is there a way to create a "mixed" octave matrix?
It sounds like you may be looking for a cell array.
Related
I am looking to find a pattern to recursively split an array to odd and even elements. I will try to describe the problem in the following:
suppose we have an array of length 16 as:
a=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
First iteration: splitting in odd and even
[0,2,4,6,8,10,12,14]
[1,3,5,7,9,11,13,15]
which basically are
a[2i] for i=0:7
a[2i+1] for i=0:7
splitting each of these arrays into odd and even elements again we have
[0,4,8,12]
[2,6,10,14]
[1,5,9,13]
[3,7,11,15]
that similarly are
4i for i=0:3
4i+2
4i+1
4i+3
splitting again the array elements would be
[0,8]
[4,12]
.
.
[1,9]
or
8i for i=0:1
8i+4
8i+2
8i+6
8+1
8i+5
8i+3
8i+1
Arrays needed to split recursively until each array has only two elements.
One things that I noticed that the bottom half is similar to the top one and we just need to add "1" to the index terms
I was wondering how Can I find the pattern for an array with an "n" elements?
Thank you very much for your time.
assuming your number n is a power of 2 (aka 2^k):
then you will have m = n/2 = 2^(k-1) arrays with following numbers for i in {0,1}:
0: m*i+f(0)
1: m*i+f(1)
...
j: m*i+f(j)
...
m-1: m*i+f(m-1)
where f(x) is a function which takes an integer (x), transforms it into an k-1-bit binary number (b), reverses it (rb) and returns its decimal value (y).
Example for k=4 (which looks a lot like your values):
x
b
rb
f(x)=y
0
000
000
0
1
001
100
4
2
010
010
2
3
011
110
6
4
100
001
1
5
101
101
5
6
110
011
3
7
111
111
7
How do i call a function that i just wrote in APL, passing in a matrix? I have already tried many things but I can't seem to find the solution. I would greatly appreciate any help.
I don't think this is a duplicate but please report if you find one :)
Thanks
Unless you declare otherwise, an APL function will accept any array as argument. For example, let's define a function which multiplies its argument by 2 and then adds 1:
∇ result←t2p1 argument
result←1+2×argument
∇
Now we define a 3-by-4 matrix of some numbers, and ask for its value:
matrix←3 4⍴3 1 4 1 5
matrix
3 1 4 1
5 3 1 4
1 5 3 1
Finally, we apply the function to the matrix:
t2p1 matrix
7 3 9 3
11 7 3 9
3 11 7 3
How to calculate the average value of a particular column in a text file with the help of Tcl Script ?
For example I have a text file containing 3 columns like:
1 2 3
4 5 6
5 9 7
3 2 8
And I want to do the average value calculation for Column 1 only; then How can I do it using Tcl script ?
Split by spaces to get the first column values
Create an empty list to store the values
Divide the sum by its length
someFile:
1 2 3
4 5 6
5 9 7
3 2 8
Hence:
values = [] # an empty list
with open(fileName, 'r') as f:
content = f.readlines()
content = [l.strip() for l in content if l.strip()] # to remove empty lines
for line in content:
values.append(int(line.split(" ")[0])) # convert str to int and append
print(sum(values) / float(len(values)))
OUTPUT:
3.25
It often happens that data will be given to you with wrapped columns. Consider, for example:
CCY Decimals CCY Decimals CCY Decimals
AUD/CAD 5 EUR/CZK 4 GBP/NOK 5
AUD/CHF 5 EUR/DKK 5 GBP/NZD 5
AUD/DKK 5 EUR/GBP 5 GBP/PLN 5
AUD/JPY 3 EUR/HKD 5 GBP/SEK 5
AUD/NOK 5 EUR/HUF 3 GBP/SGD 5
...
Which should be parsed as a dataframe of two columns (CCY and Decimals), not six. My question is, what is the most idiomatic way of achieving this?
I would have wanted something like the following:
data = pd.read_csv("file.csv")
data.groupby(axis=1,by=data.columns.map(lambda s: s.replace("\..",""))).\
apply(lambda df : df.values.flatten())
When reading the csv file we end up with columns CCY,Decimals,CCY.1,Decimals.1 .. etc. The groupby operation returns a collection of data frames:
<pandas.core.groupby.DataFrameGroupBy object at 0x3a52b10>
Which we would then flatten using numpy functionality. So we would are converting DataFrames with repeating columns into Series, and then merging these into a result DF.
However, this doesn't work. I've tried passing the different keys arguments to groupBy, but it always complains about being unable to reindex non-unique columns.
There are a number of existing questions that deal with flattening groups of columns (e.g. "Flattening" output of group.nth in Pandas), but I can't find any that do this for repeating columns.
To use groupby, I'd do:
>>> groups = df.groupby(axis=1,by=lambda x: x.rsplit(".",1)[0])
>>> pd.DataFrame({k: v.values.flat for k,v in groups})
CCY Decimals
0 AUD/CAD 5
1 EUR/CZK 4
2 GBP/NOK 5
3 AUD/CHF 5
4 EUR/DKK 5
5 GBP/NZD 5
6 AUD/DKK 5
7 EUR/GBP 5
8 GBP/PLN 5
9 AUD/JPY 3
10 EUR/HKD 5
11 GBP/SEK 5
12 AUD/NOK 5
13 EUR/HUF 3
14 GBP/SGD 5
[15 rows x 2 columns]
and then sort.
I've following problem and don't know the terminology to describe it and hence search for possible solutions.
I have a pivot table (matrix), eg each row and column have a named header. there is a defined set for rows and columns. Now let's assume that 10 rows are "combined" meaning each column is summed up to create a new "pattern".
What I would like is a way to determine alternative row combinations that lead to the same or similar "combined" pattern.
1 1 1
5 5 5
"Combined"
6 6 6
alternate row combination:
2 2 2
4 4 4
Suggestions? How is this problem called?
http://en.wikipedia.org/wiki/System_of_linear_equations#Matrix_equation
I just have to transpose above matrix to get Matrix A
[code]
1 5
1 5
1 5
[/code]
combined matrix is a vector b:
[code]
6
6
6
[/code]
and x would be just a vector full of 1.