Clear Filters
Clear Filters

Take each row in a matrix where the max number in that row is greater than 2, create new matrix. Average each column in the new matrix to result in one vector with average.

1 view (last 30 days)
I have a matrix dff_trans. I want to look through each row of the matrix and if the max value in that row greater than 2, I want to move that row to a new matrix. I then want to average each column in that new matrix to get a vector with the average at each time point (each column is a time point).
sum2z is the number of rows that have a max over 2 in the matrix dff_trans. length(dff_trans) is each time point.
c=1;
zscore_2 = zeros(sum2z,length(dff_trans));
if max(dff_trans(c,:))>2
zscore_2(c,:)=(dff_trans(c,:));
c=c+1;
end

Accepted Answer

vidyesh
vidyesh on 13 Oct 2023
Hi Caroline Szujewski,
I see that you want to generate a matrix with rows that have a maximum value greater than 2 and then calculate the average of each column in the new matrix.
You can achieve this using the below code:
B = max (dff_trans , [], 2);
C = dff_trans(B > 2, : );
D = mean(C);
  1. ‘B = max(dff_trans, [], 2);’ will create a column vector “B”, that contains the highest value from each row of the matrix dff_trans.
  2. ‘C = dff_trans(B > 2, :);’ will generate a new matrix “C”, that includes only the rows from the matrix “dff_trans” where the maximum value is greater than two.
  3. 'D = mean(C); will produce a row vector “D”, that contains the average values of each column in the matrix “C”.
You can also get your desired output with a single line of code:
D = mean( dff_trans (max( dff_trans , [], 2) > 2, : ))
Refer the following documentation to learn more about the 'max' function and how it can be used to operate on matrixes here:
Hope this answer helps.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!