Please help me create a sort function for hex strings

1 visualizzazione (ultimi 30 giorni)
Hello everyone;
Please help me create a sortedHex function which sorts all hex strings from longest to shortest hex strings , this function will compare the long hex string with other hex strings, and subtract the next short-hex string from the long one and whatever the result will be converted to zeros and added back to this short hex to be equal in length with the first Hex, do the same thing to the next hex string and subtract it from the first Hex string (the 1st long hex string) …keep do the same thing to the rest of the strings, until all hex be in the same length …. Return the result to output, this output will be called to my other add-function
Ex:
Function sortedHex= sort (length(hex1),length(hex2), length(hex3)……,length(hexn-1))
%Length(1:5) = 27,30,15,9,32 this could be (1: number or size (string))
%Index Array(1:5) = 1,2,3,4,5 this could be (1: number of string)
%Sort Array function save Indexes
%Sort (length,Index Array)
%Sorted Indexs(1:5) = 5,2,1,3,4……
%Max = SortedIndex(1);
%MaxNum = length(Max)
%MaxNum – MinNum (sorted Indexes)
Max(32) - 27 = 5 Here the 5 will be converted to 5 zeros and added to 27 to become same length as Max(pre identified longest hex-string). And basically do the same thing to the other short hex-strings
Max(32) - 30 = 2
end
Also similar to this code below:
%Case #1
A=[1,1,1,0,0,0]
B=[1,1,1,1,0,0,0]
% Make them the same size.
% If A is shorter, prepend zeros
la = length(A)
lb = length(B)
if la < lb
A = [zeros(1, lb-la), A]
end
% Do the operation for case #1:
A = A & B
% Case #2
A=[1,1,1,0,0,0]
B=[1,0,1,1,1,1,0,0,0]
% Make them the same size.
% If A is shorter, prepend zeros
la = length(A)
lb = length(B)
if la < lb
A = [zeros(1, lb-la), A]
end
% Do the operation for case #2:
A = A & B

Risposta accettata

Walter Roberson
Walter Roberson il 7 Dic 2018
  5 Commenti
Walter Roberson
Walter Roberson il 7 Dic 2018
LastN = @(V, N) V(end-N+1:end);
longest = max(@length, R);
ZPadStr = repmat('0', 1, longest)
ZPad = @(V) LastN( [ZPadStr, V], longest);
padR = cellfun(ZPad, R);
Irwin2020
Irwin2020 il 9 Dic 2018
Modificato: Irwin2020 il 9 Dic 2018
Thank you for your help...I really appreciate it, I have created my own method to pad the strings above

Accedi per commentare.

Più risposte (1)

Irwin2020
Irwin2020 il 9 Dic 2018
clc
R =["000000007FE906A41162B1720C281817AA5644BC80"
,"101010101010101017FE9016A4018D30117F0130133D8E7B8B2AF585"
,"101010101010101010101010101010101F9A013F523AF2F4427196A476F8"
,"1111111111111111113401F016B67E6548760193D6A9AB"
,"10101010101010101010101010101010101010101010101523BFC4B5011D1A76C8B"
,"FFFFFFFFFFFFFFFFFFFFFFFFFFE04F0941AF9FF0D3"];
RN=R;
NumofStrings = strlength(RN.')
MaxLengthofStr = maxk(NumofStrings,1)
newStr = pad(str,MaxLengthofStr,'left','0')

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by