How to acces Accessing alternate Columns of a Array/Data frame? - arrays

I have a matrix A of dimension say 1896x1600 Now i want to sub-sample this matrix in such a way that I get a new matrix B of dimension 1896x800 but instead of random sampling,I want the values from columns like first 100 columns and skipping the next 100 columns, i.e columns 1-100, 201-300, 401-500...1401-1500 so that I am trying to take alternate 100 columns from the matrix so that I would get a matrix B of dimension 1896x800. Any help would be appreciated thanks.

This will do the job-
in=1:size(A,2); % column indices
B1=A(:,mod(floor((in-1)/100),2)==0); % for columns 1-100,201-300,...
B2=A(:,mod(floor((in-1)/100),2)==1); % for columns 101-200,301-400,...

Related

How to take the median of the following a 4D array (31x6x2x3)?

I am trying to get the median of each row of the same columns of the following 4D array:
df = randn(31,6,2,3)
What I basically have is 31 rows (i) of 6 variables (j) for two shocks (k), all this repeated 3 times (n). Now, focus on the first shock and get a 31x6x3 array:
eg1 = squeeze(df(:,:,1,:)) %31x6x3
What I want to get is the median of every row of the same column: e.g. the first row of the first column of the 3 repetitions, then the second row of the first column of the 3 repetitions, etc. Easier to see like this:
% median of every row of the first column in the 3 repetitions
median(eg1(1,1,:))
median(eg1(2,1,:))
median(eg1(3,1,:))
...
median(eg1(31,1,:))
% median of every row of the second column in the 3 repetitions
median(eg1(1,2,:))
median(eg1(2,2,:))
median(eg1(3,2,:))
...
median(eg1(31,2,:))
% basically the median of every row of the same column for every column
This shall be done for either shocks 1 and 2.
Can anyone help me with this?
What you want is the median of the last dimension. median accepts desired dimension as input.
out=median(eg1,3)
out will be a 31x6 matrix.
If you want that from all of the egx, you just want the median of the last dimension
out=median(df,4)

Converting Rows into Columns in a cell array in MATLAB

I have a 99x1 cell array and I would like to convert it into a 33x3 cell array for example.
I would like the first 3 rows of the 99x1 cell array to make up the first row in the 33x3 cell array and then the 3rd through 6th row in the 99x1 cell array to make up the second row in the 33x3 cell array.
I also need the data when being reshaped to go over column by column before it goes down. For example I would need:
1
2
3
4
to become
1, 2; 3, 4
not
1, 3; 2, 4
Help with this would be greatly appreciated
You can simply use the reshape-function. Since reshape(yourcell,[],3) would first fill the first column and then the second and so on instead of row-wise, you will need to combine it with the transpose operator .':
newcell=reshape(yourcell,3,[]).'
This way, you will first create a 3x33 cell using the reshape and then transform it into the desired 33x3 cell. The [] tells reshape to create as many columns as needed.

Matlab: Extracting the entire row of a max value in a matrix

I have a matrix with six columns. I found the max value of a certain column but how would I go about extracting the entire row pertaining to that value?
To extract row 1 of matrix A use A([1],:) to extract row 1 and 2 use A([1,2],:)
Use the max() function as explained here. For example
if A is your matrix
[M, I] = max(A)
Row = A([I(1)],:)
where I(1) is used to find the row containing the max element of the first coloumn

Create a matrix from three column vectors

I have three column vectors, then I want to creat matrix from this column victors
A1(:);
A2(:);
A3(:)
each column vectors has 25 element then the new matrix C will be a matrix with 3x25
I want to make A1(:) the first column of matrix c
A2(:) second column
A3(:) third column
Use cat to concatenate along dimension 1 or 2 depending on how you input those three vectors.
Thus, you can use -
C = cat(2,A1(:),A2(:),A3(:)).'
Or
C = cat(1,A1(:).',A2(:).',A3(:).')
Of course, you can skip (:)'s, if you know that all those are column vectors.
The above two approaches assumes that you intend to get an output of size 3 x N, where is N is the number of elements in the column vectors. If you were looking to get an output of size N x 3 , i.e. where each column is formed from the elements of column vectors A1, A2 and so on, just drop the transpose from the first of the two approaches mentioned above. Thus, use this -
C = cat(2,A1(:),A2(:),A3(:))

MATLAB Efficiently find the row that contains two of three elements in a large matrix

I have a large matrix, let's call it A, which has dimension Mx3, e.g. M=4000 rows x 3 columns. Each row in the matrix contains three numbers, eg. [241 112 478]. Out of these three numbers, we can construct three pairs, eg. [241 112], [112 478], [241 478]. Of the other 3999 rows:
For each of the three pairs, exactly one row of M (only one) will contain the same pair. However, the order of the numbers could be scrambled. For example, exactly one row will read: [333 478 112]. No other row will have both 478 and 112. I am interested in finding the index of that row, for each of the three pairs. The output should then be another matrix, call it B, with same dimensions 4000x3, where each row has the indices of the rows in the original matrix A that share a pair of numbers.
No other row will contain the same three numbers.
Other rows may contain none of the numbers or one of the numbers.
Here's a function that accomplishes this, but is very slow - I'd like to know if there is a more efficient way. Thanks in advance!
M=size(A,1); % no elements
B=zeros(M,3);
for j=1:M
l=1;
k=1;
while l<4 % there cant be more than 3
if k~=j
s=sum(ismember(A(j,:),A(k,:)));
if s==2
B(j,l)=k;
l=l+1;
end
end
k=k+1;
end
For loops are not needed, just use ismember the following way:
row_id1=find(sum(ismember(M,[241 112]),2)>1);
row_id2=find(sum(ismember(M,[478 112]),2)>1);
row_id3=find(sum(ismember(M,[478 241]),2)>1);
each row_id will give you the row index of the pair in that line regardless of the order at which it appears.
The only assumption made here is that one of the pair numbers you look for will not appear twice in a row, (i.e. [112 333 112]). If that assumption is wrong, this can be addressed using unique.

Resources