Please help me create a sort function for hex strings
1 view (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Walter Roberson
on 7 Dec 2018
5 Comments
Walter Roberson
on 7 Dec 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);
More Answers (1)
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!