필터 지우기
필터 지우기

Please help me create a sort function for hex strings

조회 수: 2 (최근 30일)
Irwin2020
Irwin2020 2018년 12월 7일
편집: Irwin2020 2018년 12월 9일
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

채택된 답변

Walter Roberson
Walter Roberson 2018년 12월 7일
  댓글 수: 5
Walter Roberson
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);
Irwin2020
Irwin2020 2018년 12월 9일
편집: Irwin2020 2018년 12월 9일
Thank you for your help...I really appreciate it, I have created my own method to pad the strings above

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by