Find the max/min values within determined segments of a matrix
4 次查看(过去 30 天)
显示 更早的评论
For two matrices A and B:
A = [1 2 3; 4 5 6; 7 8 9; 10 11 12];
B = [3 5 8; 3 1 2; 1 5 2; 5 8 0];
For certain segments in A, I want to get the highest values related values in B. For this example, dividing the matrix into segments every 2 values of A, the output should be:
A = [1 2 3; 4 5 6; 7 8 9; 10 11 12];
Bmax = [5 5 8; 8 2 2; 5 5 5; 5 8 8];
Bmin = [3 3 3; 3 1 1; 1 1 2; 2 0 0];
To clarify the example, for values of A between 5 and 6, the maximum values of A where 2, while the minimum where 1.
Look into the comments below to get more information on the reasons for asking this question.
Thanks!
5 个评论
Hamoon
2015-9-14
Well, I hoped you'd say something about your data, and the reasons you are doing this. Actually it's not about Matlab, it's about what you want to do and how you want to do it, and the programming part of that is not an issue here. It's not hard to calculate maximum and minimum of B in that ranges, when you found that ranges you can white this into your loop:
IndexB = B<=val1 & B>=val0;
B_max_current = max(B(IndexB));
B_min_current = min(B(IndexB));
But I don't think that's your problem. It would be better if you talked about your database and the reason you're doing this. Then it's possible someone brings up with some ideas and algorithms to help you on this.
Victor Francisco Pajuelo Madrigal
2015-9-14
编辑:Victor Francisco Pajuelo Madrigal
2015-9-14
采纳的回答
Hamoon
2015-9-14
Actually you were right, MATLAB answers is directed to the programming side of the issue, I just wanted to know what you exactly want, because your example was not clear enough. So you have two matrices (A and B) with same size and different values, and you want to find minimum and maximum values of those elements in the "B" which their values lies in a specific range according to "A". So you won't have a Bmin and Bmax as it is in your example. Bmin and Bmax would be two vectors with "n" elements, where "n" is the number of ranges you chose. I changed your code a little bit. you just compare this code with your own code and tell me is that what you want? because its output is not like what you said in your example:
A = [1 2 3; 4 5 6; 7 8 9; 10 11 12];
B = [3 5 8; 3 1 2; 1 5 2; 5 8 0];
n = 6; % number of ranges
Amin=min(A(:));
Amax=max(A(:));
% initialize Bmax and Bmin
Bmax = zeros(1,n);
Bmin = zeros(1,n);
ranges = linspace(Amin,Amax+1,n+1); % split values into "n" ranges with equal size
% you may do something else to find your
% desire ranges
for i = 1 : n
indices = ( A<ranges(i+1) & A>=ranges(i) );
Bmax(i) = max(B(indices));
Bmin(i) = min(B(indices));
end
the answer is like this:
Bmax =
5 8 2 5 5 8
Bmin =
3 3 1 1 2 0
2 个评论
Victor Francisco Pajuelo Madrigal
2015-9-14
编辑:Victor Francisco Pajuelo Madrigal
2015-9-14
Hamoon
2015-9-14
My pleasure.
Actually you still can modify your code and optimize it. But there is another issue. Instead of using maximum/minimum value for each range, you can use for example "m" smallest/largest values and compute mean value or mode value for that "m" smallest/largest values (or perform any other statistical methods on them) and consider this value as smallest/largest value, this can reduce the effect of outliers.
更多回答(0 个)
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!