To find Maximum value and minimum value for each group of four rows for a 1576*1024 matrix
7 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Gani
2019-2-8
Hello,
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
end
% You can transpose if necessary
naMin';
naMax';
6 个评论
更多回答(4 个)
madhan ravi
2019-2-8
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]));
Jos (10584)
2019-2-8
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)
0 个评论
John D'Errico
2019-2-9
编辑:John D'Errico
2019-2-9
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 个评论
madhan ravi
2019-2-9
+1 John D’Errico, plus if the OP is using 2017b or later , mink() and maxk() can be used.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!