Using one array values to access another array elements Matlab - arrays

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;

Related

Qsort for a particular range in C?

I have to q sort an array but for a given range for ex.
Given array
4 5 3 7 2 1
and then i have the range 2 & 5 this means i have to sort from index 2 all the way through 5.
Resultant array
4 5 2 3 7 1
I know we can set one bound
like
qsort(array,4,sizeof(int),compa)
this will sort the array till 3rd index but will always start from index 0. I want to start the first bound by a desired value. Any Suggestions??
Just pass address to the middle of the array.
qsort(array + 2, 5 - 2, sizeof(int), compa);

Reshape and rearrange array

I have a large array, which looks like this:
1
4
5
3
6
2
7
4
3
I want to rearrange this array that it looks like this:
7 4 3
3 6 2
1 4 5
My original array has the size 13700x1, so I cannot do it manually and if I use the reshape function, the array gets shaped in the wrong way:
1 3 7
4 6 4
5 2 3
I hope my intention is clear. Thanks!
Try
tmpArray = [1
4
5
3
6
2
7
4
3]
flipud(reshape(tmpArray, 3, 3).')
x = [1,4,5,3,6,2,7,4,3]';
A = flipud(reshape(x,3,3)');
The other answers assume your vector contains a square number of elements, 4, 9, 16 .... This is true for the example vector, but not for the one you're actually working with (it's 13700x1 according to the question).
This means that the flipud(reshape()) approach will give an error:
Product of known dimensions, 3, not divisible into total number of
elements, 13924.
This is not a problem if you don't want a square matrix, as numbers that can be represented as a product of any of the numbers: 2, 5, 137.
If you want a square matrix, you need to pad the vector with zeros, NaNs or something else. This can be done the following way:
A = randi(100,13700,1); %% Random 13700x1 matrix
n = numel(A); %% Number of elements in A (13700 in this case)
elements = ceil(sqrt(n))^2; %% Number of elements needed in order to make a square matrix
B = [A; zeros(elements-n,1)]; %% Pad the vectors with zeros.
%% You can also d0 B = [A; nan(elements-n,1)];
final_matrix = flipud(reshape(B, sqrt(elements),[]).'); %% Final operation

Multiplying matrix columns

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

Merge arrays with unequal number of columns

I have around 100 1D arrays I'd like to merge to a matrix.
The arrays have 140 to 180 columns.
Is it possible to merge these 1 x (140-180) arrays to a matrix with a dimension of 100 (amount of arrays) x 180 ?
All the arrays contain numbers. I want to expand the 1x140 array to a 1x180 array by means of interpolation.
In a simplified form, it should be something like this:
A = [1 5 7 8 3]
B = [1 3 5]
result=
[1 5 7 8 3
1 2 3 4 5]
The array B (1x3) is expanded to an 1x5 matrix. And the values in between are interpolated.
Basically, I thought of using "vertcat" after all arrays are expanded by a same amount of columns.
Thanks in advance,
Koen
How about this?
array = {[1 5 7 8 3],[1 3 5]}; % example data
N = 5; % desired length (180 in your case)
aux = cellfun(#(v) interp1(linspace(0,1,length(v)),v,linspace(0,1,N)), array, 'uni', false);
result = cat(1,aux{:});
It uses linear interpolation. For your example, this gives
>> result
result =
1 5 7 8 3
1 2 3 4 5
Note that linear interpolation modifies all values of the vector except first and last, in general. For example, with N=5 the vector [1 3 4 5] would get interpolated to [1 2.5 3.5 4.25 5]. You could use other forms of interpolation by passing an extra argument to interp1, see help interp1.

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