Please help me create a sort function for hex strings
조회 수: 2 (최근 30일)
이전 댓글 표시
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
채택된 답변
Walter Roberson
2018년 12월 7일
댓글 수: 5
Walter Roberson
2018년 12월 7일
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);
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!