Multiplying matrix columns - c

I have a matrix with n rows and 3 columns, and I should multiply row n column 2 with row n column 3.
So if I have a matrix that looks like this:
1 2 3
4 5 6
7 8 9
Then I should multiply 2 with 3, 5 with 6 and 8 with 9, and create a matrix or an array that holds results:
6
30
72
How can I do that in C?

Since you are interested in learning C, an outline should do :-) The output is going to be a single column vector. Input to your function is a matrix, of some dimension p x q, and two column numbers c1 and c2. You can not skin it at least two ways.
a function that does exactly what your problem asks, iterating x[1..p][c1] and x[1..p][c2] (so loop variable will be row numbers 1..p, and multiply them, producing result[1..p]
a function that returns a column vector from a given matrix, and then another function that does the element-wise product of two vectors as above. This jimho might be a more interesting option.
HTH

Related

Using one array values to access another array elements Matlab

I am doing some 3D and 4D matrix manipulation in Matlab.
I have created a 2D array which row values contain the index values of interest in a 3D matrix.
Assuming array A of size (Nx2)
A=[2 3 1;5 6 2;7 9 3;3 3 4;1 5 5]
2 3 1
5 6 2
7 9 3
3 3 4
1 5 5
Then, I want to use these elements to manipulate matrix B of size (NxMxL)
B=rand(9,9,5);
So I want to set B(2,3,1)=0 which corresponds to A(1,:).
If I naively go B(A(1,:))=0 this doesn't return me the desired output.
What I understand is that Matlab translate this into B=B(:) which reshape the matrix into a 1xNML
and then returns me the elements 2, 3 and 1 of the reshaped matrix.
How can I avoid this and make it understand my argument B(A(1,:))=B(2,3,1)?
use sub2ind , for example zeroing all the elements in B using of the rows in A as indices:
B(sub2ind(size(B),A(:,1),A(:,2),A(:,3)))=0;

2D array minimum sum of Y elements and just two rows that we can chose to get minimum

With given 2d array[X][Y], i have to find the smallest possible sum of Y elements but:
the sum must be created by using just 2 rows,
each value must be from different index
Example:
for array
7 3 7 9
2 20 10 6
8 8 8 8
Result should be 18, as we get 3 + 7 from 1st row and 2 + 6 from 2nd.
I've been thinking about few hours but i can't figure out how to deal with it.
Try this one here.
Method 1 (Naive Approach): Check every possible submatrix in given 2D
array. This solution requires 4 nested loops and time complexity of
this solution would be O(n^4).
Method 2 (Efficient Approach): Kadane’s algorithm for 1D array can be
used to reduce the time complexity to O(n^3).

MATLAB - extract array values based on conditions

I have 4x4 matrix A
[1 2 3 4;
2 2 2 3;
5 5 5 5;
4 4 4 4]
I know how to locate all values less than 4. A<4. But I'm not sure how to write an 'if' statement for; three or more values, all which are less than 4, contained in the same row. For instance; see above A(1,:) and A(2,:) satisfies my conditions.
You can basically do A<4 to know which ones are smaller. If you want to know which rows contain N values smaller than 4 then you can do
rows=find(sum(A<4,2)>=3)
This basically does:
find smaller than 4
Count how many of them in each row (sum(_,2))
find if they are 3 or more
give the row index of those find()

Picking out elements of an array according to a specific rule

So I have a 100000-by-2 matrix in MATLAB. I want to extract only the elements in the second column which all have the same element for their corresponding row in the first column. E.g. if we have:
A = [1 2
3 4
2 6
1 5
4 1
1 3]
and then specify that we want all elements in the second column with 1 as their corresponding value in the 1st column. So I would want the above to become:
2
5
3
Does anyone know how to do this in MATLAB?
Matlab supports matrix operations, so you can do what you want without having to explicitly loop through the entire matrix like some other languages.
By using logical indexing, (more on that here https://www.mathworks.com/help/matlab/math/matrix-indexing.html?refresh=true)
you can extract the necessary elements from your matrix.
First you want to create a [0,1] array of rows in your matrix that satisfy your condition.
You can do that with
A(:,1) == 1
This will produce
1
0
0
1
0
1
Which means that the first, fourth and last rows satisfy the condition where the element in the first column of the matrix satisfies the condition that it equals one.
Now you can use this to produce the elements in the second column, by simply using it as an index of your original matrix.
A(A(:,1)==1,2)
to produce the required result.

Calling multiple values from data frame by row and column in R

I'm working in R and I'd like to call a selection of values from a data frame by their column and row indices. However doing this yields a matrix rather than an array. I shall demonstrate:
Given the data.frame:
a = data.frame( a = array(c(1,2,3,4,5,6,7,8,9), c(3,3)) )
(for those of you who don't want to plug it in, it looks like this)
a.1 a.2 a.3
1 1 4 7
2 2 5 8
3 3 6 9
And lets say I have two arrays pointing to the values I'd like to grab
grab_row = c(3,1,2)
grab_col = c(1,2,1)
Now I'd expect this to be the code I want...
a[ grab_row, grab_col ]
To get these results...
[1] 3 4 2
But that comes out as a 3x3 matrix, which makes enough sense in and of itself
a.1 a.2 a.1.1
3 3 6 3
1 1 4 1
2 2 5 2
Alright, I also see my answer is in the diagonal of the 3x3 matrix... but I'd really rather stick to an array as the output.
Any thoughts? Danka.
Passing the row and column indices in as a two-column matrix (here constructed using cbind()) will get you the elements you were expecting:
a[cbind(grab_row, grab_col)]
[1] 3 4 2
This form of indexing is documented in ?"[":
Matrices and array:
[...snip...]
A third form of indexing is via a numeric matrix with the one
column for each dimension: each row of the index matrix then
selects a single element of the array, and the result is a vector.
Try this:
> mapply(function(i,j)a[i,j], grab_row, grab_col)
[1] 3 4 2
Works for both dataframes and matrices.

Resources