How to perform sum operation to 2X2 matrix in Flutter/dart - flutter

in my dart code, I have a 2X2 matrix, like this
List<List<int>> a =
[
[10, 2],
[2, 2],
];
List<List<int>> b =
[
[1, 2],
[2, 2],
];
so, I want to perform sum operation to these two matrices without using any predefined function.
the result that I want, like this
List<List<int>> c =
[
[11, 4], // 10+1 2+2
[4, 4], // 2+2 2+2
];

this is the general answer to the sum 2D matrix
List<List<int>> c=[];
int minSize=a.length>b.length?b.length:a.length;
for(int i=0;i<minSize;i++){
List<int> temp=[];
int minListSize=a[i].length>b[i].length?b[i].length:a[i].length;
for(int j=0;j<minListSize;j++){
temp.add(a[i][j]+b[i][j]);
}
c.add(temp);
}

Well, if you know beforehand they are both 2x2 matrices, then this straightforward way is a trivial solution, which I think even beats every other solution in terms of performance.
List<List<int>> c =
[
[a[0][0] + b [0][0], a[0][1] + b [0][1]],
[a[1][0] + b [1][0], a[1][1] + b [1][1]]
];

Related

CoffeeScript - transform object into a matrix using comprehensions

I have an object of the form: {a: [1,2,3,4], b: [5,6,7,8]} and I want to transform it, using comprehensions, into an array of arrays of 3 items:
[
['a', 0, 1], ['a', 1, 2], ['a', 2, 3], ['a', 3, 4],
['b', 0, 5], ['b', 1, 6], ['b', 2, 7], ['b', 3, 8]
]
I tried this ( [x,y,v] for v, y in h for x, h of obj ) but it gives an array of two elements of 4 elements:
[
[ [], [], [], [] ],
[ [], [], [], [] ]
]
How can I skip the array of the second level?
It's easier to see when you split out the two comprehensions:
result = for x, h of obj
for v, y in h
[x,y,v]
Your result will be 2 levels deep, as each comprehension is returning an array.
First depth array will have one array for each element in your object.
Each of these arrays will contain the results for one key in your object
Best way around this is to push each of the 3 element arrays you want into a separate array.
result = []
for x, h of obj
for v, y in h
result.push [x,y,v]
Alternatively with the compact formatting:
result = []
result.push [x,y,v] for v, y in h for x, h of obj
If you already have a utility library like lodash, you could use the flatten method. But this would involve another iteration over your arrays, so not practical if performance matters and you have very large datasets

Octave function to get groups of consecutive columns in matrix

I am trying to find an efficient way of extracting groups of n consecutive columns in a matrix. Example:
A = [0, 1, 2, 3, 4; 0, 1, 2, 3, 4; 0, 1, 2, 3, 4];
n = 3;
should produce an output similar to this:
answer = cat(3, ...
[0, 1, 2; 0, 1, 2; 0, 1, 2], ...
[1, 2, 3; 1, 2, 3; 1, 2, 3], ...
[2, 3, 4; 2, 3, 4; 2, 3, 4]);
I know this is possible using a for loop, such as the following code snippet:
answer = zeros([3, 3, 3]);
for i=1:3
answer(:, :, i) = A(:, i:i+2);
endfor
However, I am trying to avoid using a for loop in this case - is there any possibility to vectorize this operation as well (using indexed expressions)?
Using just indexing
ind = reshape(1:size(A,1)*n, [], n) + reshape((0:size(A,2)-n)*size(A,1), 1, 1, []);
result = A(ind);
The index ind is built using linear indexing and implicit expansion.
Using the Image Package / Image Processing Toolbox
result = reshape(im2col(A, [size(A,1) n], 'sliding'), size(A,1), n, []);
Most of the work here is done by the im2col function with the 'sliding' option.

Octave/MATLAB: Using a matrix to access elements in a matrix without loops

Consider, the two matrices:
>> columns = [1,3,2,4]
and
>> WhichSet =
[2, 2, 1, 2;
1, 1, 2, 1;
1, 2, 1, 2;
2, 1, 2, 2]
My intent is to do the following:
>> result = [WhichSet(1,columns(1)), WhichSet(2,columns(2)), WhichSet(3, columns(3)) and WhichSet(4, columns(4))]
result = [2,2,2,2]
without any loops.
Because how indexing works, you can not just plug them as they are now, unless you use linear indexing
Your desired linear indices are:
ind=sub2ind(size(WhichSet),1:size(whichSet,1),columns);
Then
out=WhichSet(ind);

Apply a cell array of functions to a value

I define a cell array that contains values and functions:
>> A = {1, 2, 3; #(x) x+5, #(x) x+10, 5}
A =
[ 1] [ 2] [3]
#(x)x+5 #(x)x+10 [5]
Does anyone know how to apply this cell array to a value? For instance, when x = 2, the application returns another cell array:
[ 1] [ 2] [3]
[ 7] [ 12] [5]
Define your constants as functions:
A = {#(x)1, #(x)2, #(x)3; #(x) x+5, #(x) x+10, #(x)5}
now use cellfun:
k = 2;
cellfun(#(x)x(k),A)
Also note that if you want to apply multiple k values at once (e.g. k = 1:5) you will need to edit your constant functions in A from this form #(x) n to something like #(x) n*ones(size(x)) and then change the cellfun call to:
cellfun(#(x)x(k),A, 'uni',0)
To answer the question from your comments:
is it possible to refer to other cells in a function in a cell array?
For instance, can we define something like A = {#(x)1, #(x)2, the 1st cell + the 2nd cell, #(x)4}?
You define A as follows:
A = {#(x)1, #(x)2, #(x)(A{1}(x)+A{2}(x)), #(x)4}
Wouldn't it be better to define the cell-array/function thingie like this:
A = #(x) {1, 2, 3; x+5, x+10, 5};
Then you can apply it by simpliy doing
A(2)
Maybe you can even use normal matrices instead of cell array here:
A = #(x) [1, 2, 3; x+5, x+10, 5];

Copy to and delete from matrices

Not sure even if this question stands valid. But better ask.
Suppose we have two matrices in MATLAB of size (n,1) and (m,1) and we want to copy certain rows from matrix A to matrix B on a condition.
e.g. if value A(i,1) is less or equal to X
And later delete those rows from source matrix i.e. matrix A
Example:
A = [1, 2, 3, 4, 5, 6]
B = [8, 9]
copy all values which are less than or equal to 4 from A to B, and delete from A
Matrices becomes
A = [5, 6]
B = [8, 9, 1, 2, 3, 4]
You can use a logical matrix to identify the items:
mask = (A <= 4);
B = [B A(mask)];
A(mask) = [];