Maxk gives an incorrect result

Hi all,
I am trying to get 4 different value from a matrix. I am using maxk to find these values indices. Maxk can find first two maximum value with indices but when I try to find third it gives an incorrect result. for example when ı select row "1" it found 2 maximum value with their column in matrix and gives me 7-15 then I select 7 for my row it should needs to give me 8th column but it gives 1. I cannot understand why
Here is my code:
for i = nodes
if i == 1 [~,seed] = maxk(m(1:n),2);
end
end
s1 = seed(1,1);
s2 = seed(1,2);
for i = nodes
if i == 7 [~,seed2] = maxk(m(7,n),1);
end
end

 Accepted Answer

For the case that i == 1 you have:
maxk(m(1:n),2)
m(1:n)is the first n elements of m (going in column-major order, which is what MATLAB uses). For example:
m = magic(4)
m = 4×4
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
n = 6;
m(1:n)
ans = 1×6
16 5 9 4 2 11
For the case that i == 7, you have:
maxk(m(7,n),2)
m(7,n) is the element(s) of m at row 7, column(s) n. If n is a scalar, that's one element. For example:
m = magic(7)
m = 7×7
30 39 48 1 10 19 28 38 47 7 9 18 27 29 46 6 8 17 26 35 37 5 14 16 25 34 36 45 13 15 24 33 42 44 4 21 23 32 41 43 3 12 22 31 40 49 2 11 20
n = 6;
m(7,n)
ans = 11
If you want to use maxk on rows of m, then the syntax would be:
maxk(m(1,:),2) % using first row of m
maxk(m(7,:),2) % using 7th row of m
For example (using m as the 7x7 magic square still):
m(1,:)
ans = 1×7
30 39 48 1 10 19 28
m(7,:)
ans = 1×7
22 31 40 49 2 11 20
[~,seed] = maxk(m(1,:),2)
seed = 1×2
3 2
[~,seed2] = maxk(m(7,:),2)
seed2 = 1×2
4 3

6 Comments

Thank you very much. Is there a iterative way to do that. I mean I would like to create a set named seed which will have 4 value. First I would like to pick max value while row = 1 then I Would like to select a second value while row = first max value. But here is the thing the code should not consider the first selected max value when choosing the second max value. Is there a way to select four value with iterative way ?
If I understand correctly, you are finding the indexes of the 2 maximum values in row 1, so that if the maximum occurs in column 1, you use the 2nd maximum's index instead, since you don't want to use row 1 again. Is that right?
m = magic(4)
m = 4×4
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
current_row = 1;
seed = zeros(1,4);
for ii = 1:4
[~,idx] = maxk(m(current_row,:),2);
if idx(1) == current_row
current_row = idx(2);
else
current_row = idx(1);
end
seed(ii) = current_row;
end
disp(seed)
4 3 4 3
I'm not sure that's what you mean. If not, can you provide a concete example where you step through the process?
Kk
Kk on 22 Apr 2022
Edited: Kk on 22 Apr 2022
I could not explain myself cleary very sorry about that. What I am trying to say is actually first step of Grasp Algorithm which is creating a seed set. For example:
I have a distance matrix which name is dist
dist = [ 0 5 10 15
5 0 21 22
10 21 0 13
15 14 13 0];
I try to create a seed set. First value of seed set is:
[~,seed] = maxk(dist(1,:),1);
which is 4 (Value of 15). Second value of seed set needs to be 2 (value of 14).
[~,seed] = maxk(dist(4,:),1);
at the end my seed set should be 4 and 2.
I am trying to this actually. But I try to this in the same part not seperatly.
I think I see. 15 at index 1 in row 4 is the maximum, but you can't use 15 again, so you pick 14 at index 2, the second-largest element of row 4, so seed #2 is 2 not 1. Is that it?
dist = [ 0 5 10 15
5 0 21 22
10 21 0 13
15 14 13 0];
m = dist;
n_seeds = 2;
current_row = 1;
seed = zeros(1,n_seeds);
for ii = 1:n_seeds
% just use regular max (only need one max value)
% since m will have NaNs where previously used
% values occurred (see below)
[~,idx] = max(m(current_row,:));
% set any element of m equal to m(current_row,idx)
% to NaN, so it cannot be selected again by max
m(m == m(current_row,idx)) = NaN;
% next row is index of this value
current_row = idx;
% set the seed value
seed(ii) = current_row;
end
disp(seed)
4 2
That is exactly the same thing what I was trying to say and do. Thank you very much mate.
You're welcome!

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Asked:

Kk
on 22 Apr 2022

Commented:

on 23 Apr 2022

Community Treasure Hunt

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

Start Hunting!