Selecting part of a matrix in Octave to form another matrix - octave

Suppose I have the matrix
a = [1 2 3;
4 5 6;
7 8 9;]
I want to select the first two columns to form a matrix
b = [1 2;
4 5;
7 8;]
How to achieve this in Octave?
I know how to select a single column, but how to select many columns (let's say, the first 8 columns of a matrix having 16 columns) and form a matrix with them?
Also, how to select rows in a similar manner to form a matrix?

You can use the following code
b = a(:,1:2)
where : means taking all rows, and 1:2 means taking columns from 1 to 2.

Related

Removing Duplicates from a Matrix

Let's say I have a matrix, where the values in in the first column are likely to repeat. Such as
1 2
3 4
5 6
1 3
Etc...
I want to get a matrix where the duplicates are added together into one row, with the second column being the sum of the entires in the duplicate row. For example the matrix above should be
1 2+3
3 4
5 6
Etc...
Any idea how to accomplish this? Would it be better to try to edit in place or copy the values into a new matrix?
The solution to your problem is to use accumarray. This, accumulating values in a new array, is the use case for the function. It will be much faster than a for loop and more readable (well, this is arguable. Many people also find it difficult but once you understand it, it's dead easy and very powerful):
octave> x = [1 2; 3 4; 5 6; 1 3];
octave> [y, ~, j] = unique (x(:,1));
octave> [y accumarray(j, x(:,2))]
ans =
1 5
3 4
5 6
Note that the above will work even when your data is not integers. This is because we are using the last output of unique(), j, which are indices into the array of unique values y.
octave> x = [1.1 2; 1.3 4; 1.5 6; 1.1 3];
octave> [y, i, j] = unique (x(:,1));
octave> [y accumarray(j, x(:,2))]
ans =
1.1000 5.0000
1.3000 4.0000
1.5000 6.0000
If the order of the rows of the summed matrix don't matter and you only have integer values, you could use unique to check each individual value in your first column. Like this:
m = [1 2; 3 4; 5 6; 1 3]; %original matrix
mvals = unique(m(:,1)); %unique of first column
mnew = zeros(length(mvals),size(m,2)); %preallocation
for k=1:length(mvals)
mnew(k,:) = [mvals(k), sum(m(m(:,1)==mvals(k),2:end),1)]; %sum each row where first is mvals(k)
end

SSIS Not Exists In One Data Flow

I'm trying to achieve the following in SSIS:
Union All 6 separate SQL queries (OLE DB Sources) (call this dataset A)
Dataset A contains
id Col A Col B......
1
2
3
4
5
6
7
8
I have another OLE DB Source SQL query (dataset B) that contains
id Col A Col B .......
1
2
3
4
10
11
12
All columns Col A, Col B etc. are the same in all the queries.
I want to return dataset A unioned (UNION ALL) with dataset B where the ids in dataset B don't match the ids in dataset A. i.e.
id Col A Col B......
1
2
3
4
5
6
7
8
10
11
12
Also before performing the UNION there is some extra work done on dataset B to fill in NULL values in the columns.
I can achieve this quite easily by inserting dataset A into a table then using this table in a subsequent NOT EXISTS or similar query to get the missing info and then UNIONing together. Ideally though I'd like to do this in one sweep using a dual multicast with one side going into a Lookup No match and the other straight into a destination table.
Unfortunately the Lookup and No Match output returns the 'wrong' missing data i.e. it returns
5
6
7
8
rather than
10
11
12
this is because the lookup (dataset B) and source data (dataset A) are the 'wrong way' around. I don't want to rewrite dataset A as the Lookup query so is there another way around this using a different transform or other method?
I've experimented with the Cache Transform but it doesn't seem to work in the same data flow as the Lookup Transform as there is read/write contention.
Thanks,
Rich.
Sounds like you want a "Fuller Outer Join" kind of "Merge" component, then a kinda manual merge after the fact. If I misunderstood you, you can always add a Conditional Split after the Merge, to filter if A is null, if B is null, etc.
An example for the "Merge Columns" is creating a repeated "Id" with this expression: "ISNULL(Id_A) ? Id_B : Id_A"
An example for the "Full Outer Join - Merge":

KDB: apply dyadic function across two lists

Consider a function F[x;y] that generates a table. I also have two lists; xList:[x1;x2;x3] and yList:[y1;y2;y3]. What is the best way to do a simple comma join of F[x1;y1],F[x1;y2],F[x1;y3],F[x2;y1],..., thereby producing one large table?
You have asked for the cross product of your argument lists, so the correct answer is
raze F ./: xList cross yList
Depending on what you are doing, you might want to look into having your function operate on the entire list of x and the entire list of y and return a table, rather than on each pair and then return a list of tables which has to get razed. The performance impact can be considerable, for example see below
q)g:{x?y} //your core operation
q)//this takes each pair of x,y, performs an operation and returns a table for each
q)//which must then be flattened with raze
q)fm:{flip `x`y`res!(x;y; enlist g[x;y])}
q)//this takes all x, y at once and returns one table
q)f:{flip `x`y`res!(x;y;g'[x;y])}
q)//let's set a seed to compare answers
q)\S 1
q)\ts do[10000;rm:raze fm'[x;y]]
76 2400j
q)\S 1
q)\ts do[10000;r:f[x;y]]
22 2176j
q)rm~r
1b
Setup our example
q)f:{([] total:enlist x+y; x:enlist x; y:enlist y)}
q)x:1 2 3
q)y:4 5 6
Demonstrate F[x1;y1]
q)f[1;4]
total x y
---------
5 1 4
q)f[2;5]
total x y
---------
7 2 5
Use the multi-valent apply operator together with each' to apply to each pair of arguments.
q)raze .'[f;flip (x;y)]
total x y
---------
5 1 4
7 2 5
9 3 6
Another way to achieve it using each-both :
x: 1 2 3
y: 4 5 6
f:{x+y}
f2:{ a:flip x cross y ; f'[a 0;a 1] }
f2[x;y]
5j, 6j, 7j, 6j, 7j, 8j, 7j, 8j, 9j

How to create a histogram using MySQL

I am trying to create a histogram data using following query:
SELECT FLOOR(Max_Irrad/10) AS bucket, COUNT(*) AS COUNT
FROM marctest.test_summarynimish
where Lcu_name='Allegro'
and Lcu_Mode='Standard'
GROUP BY bucket;
following is the result that i am getting:
bucket count
0 3
4 3
5 12
7 6
8 3
10 3
now the bucket field is the range or bin used in the histogram. I want to create a bucket values with consistent range, for eg starting from 0,4,8,12.... and so on.. Is there any way to achieve this in mysql?
This is how I am expecting to have as result:
bucket count
0 3
4 21
8 6
I think we can use the following general form to create a general histogram:
select (x div 4) * 4 as NewX, count(*) as NewY from histogram
group by NewX
Where x is the real x value of the x axis and count(*) is the real y value. The number 4 is the size amount of the x values we want to group. This means we will group all x values in groups of 4 (e.g.: group 1 is 0, 1, 2, 3; group 2 is 4, 5, 6, 7, and so on). The count of each item in the group will become the NewY value
You can play with this here
Applying this logic to your query this would be:
select (floor(Max_Irrad/10) div 4) * 4 as NewX, count(*) as NewY
from marctest.test_summarynimish
where Lcu_name='Allegro' and Lcu_Mode='Standard'
group by NewX
Let me know if you have any trouble or doubt about this.
Just make your buckets bigger by dividing Max_Irrad by 40 instead of 10.

Is this pattern reconstitution or what is the name for this problem?

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.