excelsortingmatrixexcel-formuladiagonal

Excel sort in ascending order excluding diagonal elements


The formula below returns the least 10 values if including the diagonal values if $C$5="Y". How can we only get non-diagonal values if $C$5="Y".

=VSTACK({"V1","V2","V3","V4"},
    HSTACK({1;2;3;4;5;6;7;8;9;10},TAKE(SORT(--TEXTSPLIT(TEXTAFTER("|"&
    TOCOL(IFS(ISNUMBER(ABS(IF(ISREF(IF($C$5="N",INDIRECT("'"&$C$10&"'!B2:E5"),IF(ROW(INDIRECT("'"&$C$10&"'!B2:E5"))=COLUMN(INDIRECT("'"&$C$10&"'!B2:E5")),0,INDIRECT("'"&$C$10&"'!B2:E5")))), IF($C$5="N",INDIRECT("'"&$C$10&"'!B2:E5"),IF(ROW(INDIRECT("'"&$C$10&"'!B2:E5"))=COLUMN(INDIRECT("'"&$C$10&"'!B2:E5")),0,INDIRECT("'"&$C$10&"'!B2:E5"))), 0)-
    IF($C$5="N",INDIRECT("'"&$C$9&"'!B2:E5"),IF(ROW(INDIRECT("'"&$C$9&"'!B2:E5"))=COLUMN(INDIRECT("'"&$C$9&"'!B2:E5")),0,INDIRECT("'"&$C$9&"'!B2:E5"))))),ABS(IF(ISREF(IF($C$5="N",INDIRECT("'"&$C$10&"'!B2:E5"),IF(ROW(INDIRECT("'"&$C$10&"'!B2:E5"))=COLUMN(INDIRECT("'"&$C$10&"'!B2:E5")),0,INDIRECT("'"&$C$10&"'!B2:E5")))), IF($C$5="N",INDIRECT("'"&$C$10&"'!B2:E5"),IF(ROW(INDIRECT("'"&$C$10&"'!B2:E5"))=COLUMN(INDIRECT("'"&$C$10&"'!B2:E5")),0,INDIRECT("'"&$C$10&"'!B2:E5"))), 0)-IF($C$5="N",INDIRECT("'"&$C$9&"'!B2:E5"),IF(ROW(INDIRECT("'"&$C$9&"'!B2:E5"))=COLUMN(INDIRECT("'"&$C$9&"'!B2:E5")),0,INDIRECT("'"&$C$9&"'!B2:E5"))))&"|"&INDIRECT("'"&$C$9&"'!A2:A5")&"|"&INDIRECT("'"&$C$9&"'!B1:E1")),3),"|",{1,2,3}),"|"),,1),10)))

Sheet 2

1 2 3 4 5
1 83 37 69 80 52
2 89 44 30 64 47
3 56 39 87 88 92
4 60 38 34 35 93
5 21 75 66 47 79

Sheet 3

1 2 3 4 5
1 43 22 46 2 27
2 5 21 37 1 37
3 11 18 6 32 2
4 42 10 10 36 46
5 9 22 1 41 37

Resulting Output:

enter image description here

Desired output:

The first four rows are diagonal values and should not be there and this table should start from row 5.


Solution

  • Smallest Differences Between Two Matrices

    enter image description here

    =LET(r_address,"B2:E5",sample,10,s_ref1,C9,s_ref2,C10,yes_no,C5,
        data1,INDIRECT("'"&s_ref1&"'!"&r_address),
        data2,INDIRECT("'"&s_ref2&"'!"&r_address),
        rc,ROWS(data1),
        s,SEQUENCE(rc^2),
        h,"V"&SEQUENCE(,rc),
        rs,ROUNDUP(s/rc,),
        cs,MOD(s-1,rc)+1,
        d,TOCOL(ABS(data1-data2)),
        st,TAKE(SORT(HSTACK(d,rs,cs)),sample),
        f,IF(yes_no="y",FILTER(st,INDEX(st,,2)<>INDEX(st,,3)),st),
    VSTACK(h,HSTACK(SEQUENCE(ROWS(f)),f)))
    

    enter image description here