To find Maximum value and minimum value for each group of four rows for a 1576*1024 matrix

7 views (last 30 days)
Hello Researchers!!
I need guidance, as i have a matrix H1 of 1576*1024, which is vertical concatination of four channels, in H1 for continuous four rows it represent one frame of each channel, i need to find maximum and minmum value for every four group of rows. untill now i just get to find maximum and minimum value for each row.
kindly guide me, how can i apply 2nd loop or design function, so that i can get results for each four group of rows, and in total i will have 394 results.
H1 = vertcat(A,B,C,D)
temp_A = zeros(1576,2);
for N = 1:1:1576
temp_A(N,1) = max(H1(N,:));
temp_A(N,2) = min(H1(N,:));

Accepted Answer

Gani on 8 Feb 2019
Please try below soluition.
naMax = [];
naMin = [];
for i = 1:4:size(H1,1) % H1 is your original matrix
naMax = [naMax, max(max(H1(i:i+3, :)))]; % append values
naMin = [naMin, min(min(H1(i:i+3, :)))];% append values
% You can transpose if necessary
Amjad Iqbal
Amjad Iqbal on 9 Feb 2019
Alright, I am new user, so i'm sorry for that.
Kindly can you guide me for that query please?
to find second maximum value also for main query, how can i find second maximum value. please guide me.

Sign in to comment.

More Answers (4)

madhan ravi
madhan ravi on 8 Feb 2019
Without loop :
R=permute(AA,[2 1 3]);
H1min=squeeze(min(R,[],[1 2]));
H1max=squeeze(max(R,[],[1 2]));
Amjad Iqbal
Amjad Iqbal on 10 Feb 2019
Edited: Amjad Iqbal on 10 Feb 2019
@madhan ravi Initially i have matrix of 1576*1024 , as i mentioned in start i was just able to find maximum value and minimum value for every row.
H1 = vertcat(A,B,C,D) %% this contain 1576*1024
temp_A = zeros(1576,2); %% this is storing max and min vlue of each row.
for N = 1:1:1576
temp_A(N,1) = max(H1(N,:));
temp_A(N,2) = min(H1(N,:));
after that i have obtained 1576*2 matrix,
whcih states that 1576 max values in first column, and 1576 min values in 2nd column,
Solution from @Gani was aslo useful for my query.
But now my question is how can i find 2nd max value for each group of four rows in that 1576*1024 matrix after finding 1st max value.
i have enclosed excel file that have 1576*1024 also.
Is that useful way that to find 2nd Max value?
A = H1;
m1 = max(A, [], 2); %find the max in each row
A(bsxfun(@eq, A, m1)) = -Inf %replace the max(s) by -Inf
m2 = max(A, [], 2); %find the new max which is the second largest.
fmax = [];
smax = [];
for i = 1:4:size(A,1) % H1 is your original matrix
fmax = [fmax, max(m1(i:i+3, :))]; % append values for 1st max
smax = [smax, max(m2(i:i+3, :))];% append values for 2nd max
No offense please.thanks for evey respected researcher for valueable suggestions.
i did learn alot from you all , thanks for guidance.

Sign in to comment.

Jos (10584)
Jos (10584) on 8 Feb 2019
Here is an accumarray trick
H1 = randi(100,10,2) % sample data
Nrows = 4 ;
R = floor((0:size(H1,1)-1)/Nrows) + 1 ;
R = repmat(R(:), 1, size(H1,2)) ;
temp_A = accumarray(R(:), H1(:), [], @min)

John D'Errico
John D'Errico on 9 Feb 2019
Edited: John D'Errico on 9 Feb 2019
Please do not use flags just to ask a question.
However, you have now asked repeatedly how to solve this problem to get the min, max, and second largest element of each block of 4 rows. (There is a slight ambiguity in my mind what you meant by second largest, but the answer is trivially resolved.) This is really rather simple. You need to get used to visualizing how elements are stored in memory, what tools like reshape, permute, squeeze, etc., do to those arrays. Once you do this, you will be far ahead of using simple loops to do all of your work.
First, what would it mean if you reshaped your array to be 3-dimensional? Reshape it into a 3-d array. That is, you did this:
H1 = vertcat(A,B,C,D)
The result was a 1576x1024 array. So I presume that each of your sub-arrays was 394x1024. And therefore it looks like you want to find the minimum of rows [1,395,789,1183] and then the min of rows 2:394:1576, etc.
Instead, you want to concatenate each of those arrays as planes of a 3-d array. So just use cat.
A = rand(394,1024);
B = rand(394,1024);
C = rand(394,1024);
D = rand(394,1024);
M = cat(3,A,B,C,D);
ans =
394 1024 4
M is now 3-dimensional. Each of your original arrays is now one plane of the 3-d array.
Can you find the minimum of each of those arrays? Of course. The maximum is as easy. That is, we could write things like
But how might you find all three values, essentially at once? USE SORT!
M = sort(M,3,'ascend');
I've used the 'ascend' key there to make sure you understand how the sort will arrange things. It will sort each of those 394x1024 vectors of length 4, into ascending order.
Mmin = M(:,:,1);
Mmax = M(:,:,4);
Mmax2 = M(:,:,3);
See that no squeeze was even needed here, because
ans =
394 1024
All arrays in MATLAB implicitly have infinitely many trailing singleton dimensions, and size ignores those trailing singleton dimensions. So a 394x1024 array is actually of size 394x1024x1x1x1x1x...
Again, you need to learn how to use MATLAB as it was designed to be used. Until you do that, you will be stuck writing loops forever.
By the way, I'm wondering right now, if by second largest you mean the one next to the largest element, or the one next to the smallest element in the sort. "Second largest" might be ambiguously interpreted. So you meant one of these lines:
Mmax2 = M(:,:,2);
Mmax2 = M(:,:,3);
But I'm not sure which it was. ;-) Your choice.
Similarly, if you wanted to do the same operations on each block of rows in sequence, thus 1:4, 5:8, 9:12, etc., we would just transform the array diffierently, like this:
M = reshape(M,4,394,1024);
M = permute(M,[2 3 1]);
M = sort(M,3,'ascend');
I've written it in 3 lines there to make it clear what each line did. Again, I would not need to use squeeze, because I carefully arranged for the result to be 394x1024x4 again. In any event, no loops were needed at all, as long as you understand how MATLAB uses memory. And that is crucial to understanding how to use MATLAB well.

Amjad Iqbal
Amjad Iqbal on 9 Feb 2019
Edited: Amjad Iqbal on 9 Feb 2019
@madhan ravi Thanks for your solution, Actually my query here is that i need to find second maximum value for each multiple four rows.
As before in 1576 * 1024 matrix, I did find first maximum value to have a matrix of 394*1.
Similarly i want to find second maximum value in 1576*1024 matrix for each multiple four rows.
naMax = [];
for i = 1:4:size(H1,1) % H1 is your original matrix
naMax = [naMax, max(max(H1(i:i+3, :)))]; % append values
This one is useful to find maximum value for each four row.
I need help to find "SECOND MAXIMUM" value accordingly.


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!