Finding maximums in matrix columns

1 次查看(过去 30 天)
Hello all,
I have a little bit of a tricky problem that I am trying to solve. I am still rather new to Matlab, so I am a little bit stuck on how to approach this problem. I will try to explain with a simple example. I have a matrix and I am trying to find the maximums of the 2nd and 3rd columns between every two values of the last column. Here is an example:
Matrix=[1 2;2 2;1 2;3 3;2 3;2 3;2 4;3 4;4 4]
I then want to find the maximums for column 1 for every i and i+1, based off the last column. So the first maximum would be found for 2:3 of the last column, which would be the first 6 rows. The first maximum would then be 3.
The next maximum would then be between 3:4 based off the values in the last column. The maximum would then be 4. I would also like to find the indices of these maximums that way I can pull the location of the maximum values.
I hope this makes sense, it's rather hard to explain.

回答(2 个)

Azzi Abdelmalek
Azzi Abdelmalek 2013-11-16
编辑:Azzi Abdelmalek 2013-11-16
Edit
n=2; % first column
m=7 % last column
c1=Matrix(:,n);
c2=Matrix(:,m);
k=1;
max_val=[];
idx_val=[];
index=[];
while k<numel(c2)
idx=find(c2==c2(k) | c2==c2(k)+1);
index{end+1}=idx;
a1=c1(idx);
[ii,jj]=max(a1);
max_val(end+1)=ii;
idx_val(end+1)=jj+k-1;
k=k+numel(idx);
end
disp(max_val)
disp(idx_val)
  2 个评论
Christopher
Christopher 2013-11-16
How would I do this if I had a matrix that was say 1000 x 7. So it has 7 columns and I want to base it off of the last column and find the maximum values for column 2?
thank you

请先登录,再进行评论。


Roger Stafford
Roger Stafford 2013-11-16
编辑:Roger Stafford 2013-11-16
Here's another way. Let A be your two-columned array. (Corrected)
f = find([true;diff(A(:,2))~=0;true]);
n = length(f)-2;
if n > 0
M = zeros(n,1);
I = zeros(n,1);
for k = 1:n
[mx,ix] = max(A(f(k):f(k+2)-1,1));
M(k) = mx;
I(k) = ix;
end
else
[M,I] = max(A(:,1));
end
M is a column vector of the maximum values and I their corresponding indices in A.

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by