10 views (last 30 days)

Hello,

I aim to create a label matrix that works as the following:

suppose I have the samples (1,2,3,4) as vectors and I want to say if any two are neighbors so the label is (1). if any two are not neighbors so the label is (-1). Note that neighbors means these two samples are of the same class. for example, the samples 1 and 3 belong to the same class and the samples 2 and 4 belong to the same class. Now, since the samples 1 and 3 of the same class so the label is (1). The samples 2 and 4 of the same class so the label is (1). The samples 1 and 4 is not of the same class so the label is (-1). The samples 3 and 2 is not of the same class so the label is (-1). In the matrix if the pair like (1,1) or (2,2).. so put zero. finally, the matrix should be:

[0 -1 1;-1 0 -1;1 -1 0 ]

So, any ideas to do that please.

Regards,

dpb
on 26 Jul 2018

Edited: dpb
on 26 Jul 2018

One possible solution--

>> class=categorical({'A','B','A','B'});

>> nk=nchoosek(1:4,2);

>> arrayfun(@(ix1,ix2) isequal(class(ix1),class(ix2)),nk(:,1),nk(:,2))

ans =

6×1 logical array

0

1

0

0

1

0

>>

Or, for your scaling,

>> ~arrayfun(@(ix1,ix2) isequal(class(ix1),class(ix2)),nk(:,1),nk(:,2))*2-1

ans =

1

-1

1

1

-1

1

>>

You can fill in the square array if desired, of course.

dpb
on 31 Jul 2018

nk is the list (by row) of four things taken two at a time so arrayfun just passes each pair to the anonymous function that is the functional form of the equality expression class(i)==class(j).

arrayfun is the one-line equivalent of writing an explicit for loop over the range of the indices 1:numel() of the variables in the argument list.

NB: I stuck with previous variable names; @G's correct in that 'class' isn't good to alias as general practice.

Guillaume
on 26 Jul 2018

Going with dpb's example (with slightly better variable names, don't use class it's too useful a function to shadow!), to generate the label matrix:

varclass = categorical({'A','B','A','B'});

labelmatrix = 2 * (varclass == varclass') - 1;

labelmatrix(logical(eye(numel(varclass)))) = 0

dpb
on 26 Jul 2018

" class it's too useful a function..."

Very good point; being a dinosaur I don't write OO code so didn't even think of it... :)

Guillaume
on 31 Jul 2018

dpb
on 31 Jul 2018

Well, there's that use of class, too... :)

Had been in a conversation over on comp.lang.fortran comparing/contrasting newer features in it to C++ and had those kinds of classes on the brain at the time...

Opportunities for recent engineering grads.

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

Start Hunting!
## 4 Comments

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/412228-how-to-create-pairwise-label-matrix#comment_593459

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/412228-how-to-create-pairwise-label-matrix#comment_593459

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/412228-how-to-create-pairwise-label-matrix#comment_593471

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/412228-how-to-create-pairwise-label-matrix#comment_593471

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/412228-how-to-create-pairwise-label-matrix#comment_593492

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/412228-how-to-create-pairwise-label-matrix#comment_593492

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/412228-how-to-create-pairwise-label-matrix#comment_595110

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/412228-how-to-create-pairwise-label-matrix#comment_595110

Sign in to comment.