Please help me create a sort function for hex strings

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일

1 개 추천

댓글 수: 5

There are many many example functions on Mathworks.com
function function_with_no_inputs_or_outputs
disp('hello')
end
function output = function_with_no_inputs_and_one_output
output = 'hello';
end
function function_with_one_input_and_no_outputs(first_input)
fprintf('The one input was: ');
disp(first_input);
end
function output = function_with_one_input_and_one_output(first_input)
output = ['The one input was: ', char(first_input)];
end
function [out1, out2] = function_with_no_inputs_and_two_outputs
out1 = 1; out2 = 'missississippi';
end
function function_with_two_inputs_and_no_outputs(in1, in2)
display(in1)
display(in2)
end
function function_with_many_inputs_and_no_outputs(varargin)
fprintf('Number of input arguments is: %d\nThey were:\n', nargin);
celldisp(varargin)
end
Notice that in no case is there an expression such as length(hex2) as an input argument in the function statement.
My friend , I have already createed a addHexNumber function, basiclley I know what a function is!, I write them myself , also im not that pretty good in Matlab either! below is my function:
function [ result ] = addHexNumber( hex1,hex2 )
% Clear the Command Windo everytime I click Run!
% hex1 = 'AA102FDE2FDEAA1012341234AAAAAEEDD6';
% hex2 = '2FDEAA10AA102FDE1234123401234FDEAA';
size = length(hex1);
result="";
carry = 0;
for i = size:-1:1 %The index variable i starts at size, then decreases in steps of 1 until it reaches 1.
h1 =typecast(uint64(hex2dec(hex1(i))), 'int64');
h2 =typecast(uint64(hex2dec(hex2(i))), 'int64');
bothSum = h1 + h2+carry; %Each time through the loop the value of carry is added to h1and h2
if bothSum > 16 % If the sum of hi,h2,and the carry greater than 16 then bothSum - 16.
bothSum = bothSum - 16;
carry = 1;
end
add = dec2hex(typecast(bothSum, 'uint64')); % convert decimal 2 unsinged Hex int
result = result+add;
end
if carry ~=0 % is the addtion reminder not equal to zero ie 1
result = result+carry; % add the reminder (carry) to result
end
result=reverse(result); % Revered the answer so it can match the sum of orignal Hex value
end
the issue is that, I have an Array of hex strings named R:
R = {'000000000000000000000000000000000000000000';
;'000000007FE906A41162B1720C281817AA5644BC80'
;'FFFFFFFFFFFFFFFFE6604FFCF6DA1466C024D53905'
;'0000000000000000023FEF553317DFDBA061CE3DF3'
;'00000000000000000000000523CE74ABBF21076BB8'
;'FFFFFFFFFFFFFFFFFFFFFFFFFFE04F0941AF9FF0D3'};
and when I add it up through my for-loop I get the result below :
R = {'000000007FE906A41162B1720C281817AA5644BC80';
;'101010101010101017FE9016A4018D30117F0130133D8E7B8B2AF585'
;'101010101010101010101010101010101F9A013F523AF2F4427196A476F8'
;'1111111111111111113401F016B67E6548760193D6A9AB'
;'10101010101010101010101010101010101010101010101523BFC4B5011D1A76C8B'
;'FFFFFFFFFFFFFFFFFFFFFFFFFFE04F0941AF9FF0D3'};
my gol is to add zeros before the hex strings and make them equal in length and then add them Up
Thanks for your input..
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일

0 개 추천

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

카테고리

도움말 센터File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

질문:

2018년 12월 7일

편집:

2018년 12월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by