To find Maximum value and minimum value for each group of four rows for a 1576*1024 matrix
Afficher commentaires plus anciens
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,:));
end
Réponse acceptée
Plus de réponses (4)
madhan ravi
le 8 Fév 2019
Without loop :
[r,c]=size(H1);
AA=reshape(H1',c,4,[]);
R=permute(AA,[2 1 3]);
H1min=squeeze(min(R,[],[1 2]));
H1max=squeeze(max(R,[],[1 2]));
13 commentaires
Stephen23
le 8 Fév 2019
+1 neat
madhan ravi
le 8 Fév 2019
Thank you!
John D'Errico
le 8 Fév 2019
Modifié(e) : John D'Errico
le 8 Fév 2019
+1. Yes. This is a nice solution. Note that it uses the vecdim version of the third argument for min and max, where it finds the min & max over 2 of the dimensions of the array. I dont know when that capability was introduced, so possibly older releases of MATLAB would not find that capability.
In older releases of MATLAB I would have reshaped those two dimensions of the array into one longer dimension, then use min and max as desired.
madhan ravi
le 8 Fév 2019
Thank you John D'Errico for versions prior to 2018b multiple calls to min and max would suffice:
H1min=squeeze(min(min(R)));
H1max=squeeze(max(max(R)));
Gani
le 8 Fév 2019
This solution is way faster , however our(mine + Ravi's) solution works only when the number of row is divisible by 4. Answer from Jos (10584) is more general and works for all cases.
John D'Errico
le 9 Fév 2019
While Jos gave a solution that is indeed more general, it has been clearly stated that there are ALWAYS a multiple of 4 rows.
Amjad Iqbal
le 9 Fév 2019
madhan ravi
le 9 Fév 2019
What should the final size be now?
madhan ravi
le 9 Fév 2019
Amjad Iqbal
le 9 Fév 2019
madhan ravi
le 10 Fév 2019
Modifié(e) : madhan ravi
le 10 Fév 2019
The file that you uploaded has 2 columns and 1575 ROWS!!!. What do they represent?? Your questions says:
>> 1576*1024
%rows-^ ^----columns
ans =
1613824
Whereas your file has:
>> 1575*2
ans =
3150
So why are you confusing the readers? And if you didn't get the answer to your question then why accept an answer? If an answer is accepted it means the question is answered.
madhan ravi
le 10 Fév 2019
Modifié(e) : madhan ravi
le 10 Fév 2019
Amjad Iqbal
le 10 Fév 2019
Modifié(e) : Amjad Iqbal
le 10 Fév 2019
Jos (10584)
le 8 Fév 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
le 9 Fév 2019
Modifié(e) : John D'Errico
le 9 Fév 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);
size(M)
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
Mmin=min(M,[],3);
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
size(Mmin)
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.
1 commentaire
madhan ravi
le 9 Fév 2019
+1 John D’Errico, plus if the OP is using 2017b or later , mink() and maxk() can be used.
Amjad Iqbal
le 9 Fév 2019
Modifié(e) : Amjad Iqbal
le 9 Fév 2019
0 votes
Catégories
En savoir plus sur Matrix Indexing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!