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

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

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)

How do I append a Column to a MATLAB Table based on certain conditions?

I have a MATLAB Table of dimensions 5000x6. I would like to add a new column to the MATLAB Table based on certain conditions for the 6th Column in the original table.
I want the following:-
1. If the 6th Column value is less than x or greater than y: Append a 1 to the new 7th Column of the MATLAB Table
2. Else, append a 0 to the new 7th Column of the MATLAB Table
So, finally, what I will have is a 5000x7 Table, with the last(7th column) consisting of 0s and 1s based on the values in the original (6th) column. I am new to working with Tables and Iterations in MATLAB, and I tried looking at other questions on Stackoverflow but couldn't get any idea of how I can do this stuff. Any help in this regard will be highly appreciated.
Thanks!
In the following I imagine you have extracted your sixth column from your data matrix. I've called it A. Then you define upper and lower bounds for you logical one indexing.
Then you create your new column by checking each element in your vector against your bounds and finally add it to your matrix.
% Sixth column
A = [1; 2; 3; 4; 5; 3; 1; 2; 8];
y= 4; % Upper boundary
x= 2; % Lower boundary
% Create logical column vector by information of sixth column
new_column = (A<x) | (A>y);
% Add new column to Matrix
A = [A new_column];
I just created some random bounds and values for your vector.

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

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,...

Adding multiple rows in Array

I have an array A size of 16X16 and I want to add first 3 rows out of 16 in A. What is the most efficient solution in MATLAB?
I tried this code but this is not efficient because I want to extend it for large arrays:
filename = 'n1.txt';
B = importdata(filename);
i = 1;
D = B(i,:)+ B(i+1,:)+ B(i+2,:);
For example, if I want to extend this for an array of size 256x256 and I want to extract 100 rows and add them, how I will do this?
A(1:3,:);%// first three rows.
This uses the standard indices of matrix notation. Check Luis's answer I linked for the full explanation on indices in all forms. For summing things:
B = A(1:100,:);%// first 100 rows
C = sum(B,1);%// sum per column
D = sum(B,2);%// sum per row
E = sum(B(:));%// sum all elements, rows and columns, to a single scalar

finding index of rows in matlab

i have a matrix B of N*3 dim. I want to find the indices of B whose column 3 has value 1.
I used the command [~,id]=ismember(1,B(:,3)). id returns only value 1 even though there are many rows in the matrix which has the column 3 with value 1. Can any one point out what is wrong in the command?
Rather do:
id = find(B(:,3)==1)
but as an aside, to use ismember you should swap your input [~,id]=ismember(B(:,3),1).

Resources