{"group":{"id":1,"name":"Community","lockable":false,"created_at":"2012-01-18T18:02:15.000Z","updated_at":"2026-04-06T14:01:22.000Z","description":"Problems submitted by members of the MATLAB Central community.","is_default":true,"created_by":161519,"badge_id":null,"featured":false,"trending":false,"solution_count_in_trending_period":0,"trending_last_calculated":"2026-04-06T00:00:00.000Z","image_id":null,"published":true,"community_created":false,"status_id":2,"is_default_group_for_player":false,"deleted_by":null,"deleted_at":null,"restored_by":null,"restored_at":null,"description_opc":null,"description_html":null,"published_at":null},"problems":[{"id":955,"title":"ismember: Enhanced Time Performance for 'rows'  - Speed Scoring (90% savings)","description":"The Challenge is to perform very fast the 'ismember' function for a long and wide array.\r\n\r\nThe data is small integer representing data permutations of items like DNA and Rubik's cube faces and orientations.\r\n\r\n*Input:* Array of uint8 of dimensions (m, 16) with values 0:3\r\n\r\n*Output:* Array Equivalent to ismember(A,B,'rows')\r\n\r\n*Hints:*\r\n\r\n1) Columns can be merged to form a reduced number of columns\r\n2) Unique has the option to provide an Array and a sorting Index","description_html":"\u003cp\u003eThe Challenge is to perform very fast the 'ismember' function for a long and wide array.\u003c/p\u003e\u003cp\u003eThe data is small integer representing data permutations of items like DNA and Rubik's cube faces and orientations.\u003c/p\u003e\u003cp\u003e\u003cb\u003eInput:\u003c/b\u003e Array of uint8 of dimensions (m, 16) with values 0:3\u003c/p\u003e\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e Array Equivalent to ismember(A,B,'rows')\u003c/p\u003e\u003cp\u003e\u003cb\u003eHints:\u003c/b\u003e\u003c/p\u003e\u003cp\u003e1) Columns can be merged to form a reduced number of columns\r\n2) Unique has the option to provide an Array and a sorting Index\u003c/p\u003e","function_template":"function idx = ismember_fast_rows(a,b)\r\n idx=ismember(a,b,'rows');\r\nend","test_suite":"%%\r\nfeval(@assignin,'caller','score',40000);\r\n%%\r\n% Functionality Tests\r\nL=128;\r\na=randi(4,L,16,'uint8')-1;\r\nb=randi(4,L,16,'uint8')-1;\r\nassert(isequal(ismember(a,b,'rows'),ismember_fast_rows(a,b)))\r\n\r\nb=a;\r\nassert(isequal(ismember(a,b,'rows'),ismember_fast_rows(a,b)))\r\n\r\nL=256;\r\na=randi(4,L,16,'uint8')-1;\r\nb=randi(4,L,16,'uint8')-1;\r\na(16:32,:)=b(32:48,:);\r\nassert(isequal(ismember(a,b,'rows'),ismember_fast_rows(a,b)))\r\n\r\n%%\r\nL=4000000;  % ismember 40    fast 5.2\r\n% 34 sec 4M\r\ntic\r\na=randi(4,L,16,'uint8')-1;\r\nb=randi(4,L,16,'uint8')-1;\r\ntoc\r\n\r\n\r\nta=clock;\r\nidx = ismember_fast_rows(a,b);\r\nt1=etime(clock,ta)*1000;\r\n\r\nfprintf('Elapsed time = %.0f msec\\n',t1)\r\n\r\n%assert(isequal(ismember(a,b,'rows'),idx))\r\n\r\nt2=min(40000,t1); % ismember scores 40000 msec\r\nfeval(@assignin,'caller','score',floor(t2));\r\n","published":true,"deleted":false,"likes_count":0,"comments_count":6,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":19,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2012-09-24T02:05:42.000Z","updated_at":"2025-06-08T09:02:05.000Z","published_at":"2012-09-24T05:39:06.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe Challenge is to perform very fast the 'ismember' function for a long and wide array.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe data is small integer representing data permutations of items like DNA and Rubik's cube faces and orientations.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eInput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Array of uint8 of dimensions (m, 16) with values 0:3\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eOutput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Array Equivalent to ismember(A,B,'rows')\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eHints:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e1) Columns can be merged to form a reduced number of columns 2) Unique has the option to provide an Array and a sorting Index\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":1789,"title":"Adding and Subtracting UINT variables","description":"Given UINT class variables output the correct solution to A-B+C.\r\n\r\n*Input:* A,B,C  Three uint variables \r\n\r\n*Output:* solution to A-B+C \r\n\r\n*Note:* Negative answer results in an expected Zero for class uint","description_html":"\u003cp\u003eGiven UINT class variables output the correct solution to A-B+C.\u003c/p\u003e\u003cp\u003e\u003cb\u003eInput:\u003c/b\u003e A,B,C  Three uint variables\u003c/p\u003e\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e solution to A-B+C\u003c/p\u003e\u003cp\u003e\u003cb\u003eNote:\u003c/b\u003e Negative answer results in an expected Zero for class uint\u003c/p\u003e","function_template":"function s=uint_function(A,B,C)\r\n  s=A-B+C;\r\nend","test_suite":"%%\r\nA=uint32(5);\r\nB=uint32(6);\r\nC=uint32(4);\r\ns_exp=3;\r\ns=uint_function(A,B,C);\r\nassert(isequal(s,s_exp),sprintf('s = %3i',s))\r\n%%\r\nA=uint32(7);\r\nB=uint32(6);\r\nC=uint32(12);\r\ns_exp=13;\r\ns=uint_function(A,B,C);\r\nassert(isequal(s,s_exp),sprintf('s = %3i',s))\r\n%%\r\nA=uint16(1);\r\nB=uint16(6);\r\nC=uint16(5);\r\ns_exp=0;\r\ns=uint_function(A,B,C);\r\nassert(isequal(s,s_exp),sprintf('s = %3i',s))\r\n%%\r\nA=uint64(12);\r\nB=uint64(14);\r\nC=uint64(3);\r\ns_exp=1;\r\ns=uint_function(A,B,C);\r\nassert(isequal(s,s_exp),sprintf('s = %3i',s))\r\n%%\r\nA=uint8(4);\r\nB=uint8(8);\r\nC=uint8(4);\r\ns_exp=0;\r\ns=uint_function(A,B,C);\r\nassert(isequal(s,s_exp),sprintf('s = %3i',s))\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":1,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":48,"test_suite_updated_at":"2013-08-12T19:33:57.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2013-08-12T19:19:02.000Z","updated_at":"2026-03-06T12:18:48.000Z","published_at":"2013-08-12T19:33:57.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eGiven UINT class variables output the correct solution to A-B+C.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eInput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e A,B,C Three uint variables\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eOutput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e solution to A-B+C\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eNote:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Negative answer results in an expected Zero for class uint\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":808,"title":"Hamming Weight - Fast","description":"The Hamming Weight, \u003chttp://en.wikipedia.org/wiki/Hamming_weight wiki Hamming Weight\u003e, in its most simple form is the number of ones in the binary representation of a value.\r\n\r\nThe task here is to create a fast Hamming Weight function/method such that processing many 4K x 4K images of 32 bit integer can be evaluated rapidly. Saw this question posed on Stack Overflow.\r\n\r\n*Input:* Vector of length N of 32 bit integer values.\r\n\r\n*Output:* Vector of number of ones of the binary representation\r\n\r\n*Scoring:* Time in milliseconds to process a [4096*4096,1] vector\r\n\r\n*Examples:* Input [7 ; 3], output=[3;2];  [16 32], output [1;1]; [0 4294967295] output [0;32]\r\n\r\n*Timing Test vector:* uint32(randi(2^32,[4096*4096,1])-1)\r\n\r\n*Minimum vector length/increment:* 65536\r\n\r\nHelpful, possibly, global variables.\r\n\r\nb1=uint32(1431655765); b2=uint32(858993459); b3=uint32(252645135) b4=uint32(16711935); b5=uint32(65535);\r\n\r\nHex: b1=55555555 b2=33333333 b3=0F0F0F0F b4=00FF00FF b5=0000FFFF \r\n\r\nThe array num_ones is created for values 0-65535 (0:2^16-1).\r\nnum_ones(1)=0, num_ones(2)=1, num_ones(3)=1,num_ones(4)=2,...num_ones(65536)=15\r\n\r\nDue to lack of zero indexing num_ones(value+1) is number of ones for value.\r\n\r\n\r\nHint: Globals are not good for time performance.\r\n\r\nHint: Segmentation appears to provide significant time optimization potential.\r\n\r\n\r\n\r\n","description_html":"\u003cp\u003eThe Hamming Weight, \u003ca href=\"http://en.wikipedia.org/wiki/Hamming_weight\"\u003ewiki Hamming Weight\u003c/a\u003e, in its most simple form is the number of ones in the binary representation of a value.\u003c/p\u003e\u003cp\u003eThe task here is to create a fast Hamming Weight function/method such that processing many 4K x 4K images of 32 bit integer can be evaluated rapidly. Saw this question posed on Stack Overflow.\u003c/p\u003e\u003cp\u003e\u003cb\u003eInput:\u003c/b\u003e Vector of length N of 32 bit integer values.\u003c/p\u003e\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e Vector of number of ones of the binary representation\u003c/p\u003e\u003cp\u003e\u003cb\u003eScoring:\u003c/b\u003e Time in milliseconds to process a [4096*4096,1] vector\u003c/p\u003e\u003cp\u003e\u003cb\u003eExamples:\u003c/b\u003e Input [7 ; 3], output=[3;2];  [16 32], output [1;1]; [0 4294967295] output [0;32]\u003c/p\u003e\u003cp\u003e\u003cb\u003eTiming Test vector:\u003c/b\u003e uint32(randi(2^32,[4096*4096,1])-1)\u003c/p\u003e\u003cp\u003e\u003cb\u003eMinimum vector length/increment:\u003c/b\u003e 65536\u003c/p\u003e\u003cp\u003eHelpful, possibly, global variables.\u003c/p\u003e\u003cp\u003eb1=uint32(1431655765); b2=uint32(858993459); b3=uint32(252645135) b4=uint32(16711935); b5=uint32(65535);\u003c/p\u003e\u003cp\u003eHex: b1=55555555 b2=33333333 b3=0F0F0F0F b4=00FF00FF b5=0000FFFF\u003c/p\u003e\u003cp\u003eThe array num_ones is created for values 0-65535 (0:2^16-1).\r\nnum_ones(1)=0, num_ones(2)=1, num_ones(3)=1,num_ones(4)=2,...num_ones(65536)=15\u003c/p\u003e\u003cp\u003eDue to lack of zero indexing num_ones(value+1) is number of ones for value.\u003c/p\u003e\u003cp\u003eHint: Globals are not good for time performance.\u003c/p\u003e\u003cp\u003eHint: Segmentation appears to provide significant time optimization potential.\u003c/p\u003e","function_template":"function y = Ham(x)\r\n% Input uint32\r\nglobal num_ones b1 b2 b3 b4 b5\r\n  y = x;\r\nend","test_suite":"%%\r\nfeval(@assignin,'caller','score',2000);\r\n%%\r\nglobal num_ones b1 b2 b3 b4 b5 net_time\r\nnet_time=2000; % default in case of time out (not needed)\r\n\r\nb1=uint32(1431655765); \r\nb2=uint32(858993459);\r\nb3=uint32(252645135);\r\nb4=uint32(16711935);\r\nb5=uint32(65535);\r\n \r\nnum_ones=uint32(zeros(65536,1)); \r\nfor i=0:65535  num_ones(i+1)=length( find( bitget( i, 1:32 ) ) ) ; \r\nend % Cody 0.996 sec\r\n%%\r\nglobal num_ones b1 b2 b3 b4 b5 net_time\r\n\r\nw=uint32(randi(2^32,[65536*1,1])-1); \r\nfor i=1:4 % Clear timing\r\n  vw=Ham(w);\r\nend\r\n\r\nwexpect=num_ones(mod(w,65536)+1)+num_ones(floor(double(w)/65536)+1); %1.56\r\nt0=clock;\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\ndt=etime(clock,t0)*250*1000; % avg of 4 runs in us\r\nfprintf('Time to execute 65536 values %.0f usec\\n',dt);\r\nassert(isequal(wexpect,vw),sprintf('Time to execute 65536 values %.0f usec\\n',dt))\r\n%%\r\nglobal num_ones b1 b2 b3 b4 b5 net_time\r\n\r\nw=uint32(randi(2^32,[65536*1,1])-1); \r\n\r\n vw=Ham(w); % Three cycles of smaller vector\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n\r\nw=uint32(randi(2^32,[4096*4096,1])-1);\r\nwexpect=num_ones(mod(w,65536)+1)+num_ones(floor(double(w)/65536)+1); %1.56\r\n\r\n \r\n  vw=Ham(w); % Big Prep file\r\n  vw=Ham(w); % Big Prep file\r\n \r\nt0=clock;\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\nnet_time=etime(clock,t0)*250; % avg of 4 runs\r\nfprintf('Time to execute 4096*4096 values %.0f msec\\n',net_time);\r\n\r\nassert(isequal(wexpect,vw),sprintf('Time to execute 4096*4096 values %.0f msec\\n',net_time))\r\n%%\r\nglobal net_time\r\n% net_time in ms\r\n% Create graph data\r\nnet_time=min(2000,net_time); % Limit graph y-axis\r\n\r\nfeval(@assignin,'caller','score',floor(net_time));\r\n\r\n%fh=fopen('Ham.m','wt');\r\n%fprintf(fh,'%s\\n',repmat('1;',[1,round(net_time/2)]));\r\n%fclose(fh);","published":true,"deleted":false,"likes_count":0,"comments_count":1,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":18,"test_suite_updated_at":"2012-11-22T11:18:57.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2012-06-30T05:08:08.000Z","updated_at":"2026-02-09T12:51:11.000Z","published_at":"2012-07-01T05:19:12.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe Hamming Weight,\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://en.wikipedia.org/wiki/Hamming_weight\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ewiki Hamming Weight\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e, in its most simple form is the number of ones in the binary representation of a value.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe task here is to create a fast Hamming Weight function/method such that processing many 4K x 4K images of 32 bit integer can be evaluated rapidly. Saw this question posed on Stack Overflow.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eInput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Vector of length N of 32 bit integer values.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eOutput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Vector of number of ones of the binary representation\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eScoring:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Time in milliseconds to process a [4096*4096,1] vector\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eExamples:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Input [7 ; 3], output=[3;2]; [16 32], output [1;1]; [0 4294967295] output [0;32]\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eTiming Test vector:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e uint32(randi(2^32,[4096*4096,1])-1)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eMinimum vector length/increment:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e 65536\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHelpful, possibly, global variables.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eb1=uint32(1431655765); b2=uint32(858993459); b3=uint32(252645135) b4=uint32(16711935); b5=uint32(65535);\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHex: b1=55555555 b2=33333333 b3=0F0F0F0F b4=00FF00FF b5=0000FFFF\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe array num_ones is created for values 0-65535 (0:2^16-1). num_ones(1)=0, num_ones(2)=1, num_ones(3)=1,num_ones(4)=2,...num_ones(65536)=15\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eDue to lack of zero indexing num_ones(value+1) is number of ones for value.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHint: Globals are not good for time performance.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHint: Segmentation appears to provide significant time optimization potential.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":1148,"title":"Compression: Encode and Decode","description":"This Challenge is to compress an array created by randi(2^32-1,1000) and then decompress your data file.\r\n\r\nThe first call will provide the array x. The output is the filename of the data encoded file. \r\n\r\nThe second call will provide the filename. The output is the numeric array by decoding the file.\r\n\r\n\r\n\r\n*Success:* File size less than 4,100,000 bytes and a correct decoded array.\r\n\r\n*Scoring:* Scaled file size.\r\n\r\n*Encode Call:*\r\n\r\n*Input:* x % randi(2^32-1,1000)\r\n\r\n*Output:* filename\r\n\r\n.\r\n.\r\n\r\n*Decode Call:*\r\n\r\n*Input:* filename\r\n\r\n*Output:* x_array\r\n  \r\nNote: global and persistent are not allowed. Unexpected files will cause a Virus Detect Failure.","description_html":"\u003cp\u003eThis Challenge is to compress an array created by randi(2^32-1,1000) and then decompress your data file.\u003c/p\u003e\u003cp\u003eThe first call will provide the array x. The output is the filename of the data encoded file.\u003c/p\u003e\u003cp\u003eThe second call will provide the filename. The output is the numeric array by decoding the file.\u003c/p\u003e\u003cp\u003e\u003cb\u003eSuccess:\u003c/b\u003e File size less than 4,100,000 bytes and a correct decoded array.\u003c/p\u003e\u003cp\u003e\u003cb\u003eScoring:\u003c/b\u003e Scaled file size.\u003c/p\u003e\u003cp\u003e\u003cb\u003eEncode Call:\u003c/b\u003e\u003c/p\u003e\u003cp\u003e\u003cb\u003eInput:\u003c/b\u003e x % randi(2^32-1,1000)\u003c/p\u003e\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e filename\u003c/p\u003e\u003cp\u003e.\r\n.\u003c/p\u003e\u003cp\u003e\u003cb\u003eDecode Call:\u003c/b\u003e\u003c/p\u003e\u003cp\u003e\u003cb\u003eInput:\u003c/b\u003e filename\u003c/p\u003e\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e x_array\u003c/p\u003e\u003cp\u003eNote: global and persistent are not allowed. Unexpected files will cause a Virus Detect Failure.\u003c/p\u003e","function_template":"function P = encode_decode(x)\r\n% P is the file_name string if x is an array\r\n% P is an array if x is the filename to decode\r\n P=x;\r\nend","test_suite":"%%\r\nfeval(@assignin,'caller','score',8000);\r\n%%\r\nfiletext = fileread('encode_decode.m');\r\n% Avoid Size 0 solutions\r\nassert(isempty(strfind(filetext, 'persistent')))\r\nassert(isempty(strfind(filetext, 'global')))\r\n%%\r\nx=randi(2^32-1,1000);\r\n\r\n\r\ndirfiles_orig=dir;\r\nnum_orig=size(dirfiles_orig,1);\r\n\r\nfname=encode_decode(x);\r\n\r\ndirfiles=dir;\r\nnum_encode=size(dirfiles,1);\r\n\r\nc=dir(fname);\r\nfprintf('Bytes %i\\n',c.bytes)\r\nfile_size=floor(c.bytes/1000);\r\n\r\nassert(num_encode==num_orig+1,'Virus Detected');\r\n\r\n\r\nxout=encode_decode(fname);\r\n\r\n\r\nassert(file_size\u003c4100,sprintf('File Size %i\\n',file_size)) \r\nassert(isequal(x,xout),'xout ~= x')\r\n\r\nfeval(  @assignin,'caller','score',floor(min( 8000,file_size ))  );\r\n\r\n\r\n","published":true,"deleted":false,"likes_count":2,"comments_count":0,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":12,"test_suite_updated_at":"2012-12-30T03:12:39.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2012-12-28T22:43:52.000Z","updated_at":"2012-12-30T03:21:48.000Z","published_at":"2012-12-28T23:29:43.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThis Challenge is to compress an array created by randi(2^32-1,1000) and then decompress your data file.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe first call will provide the array x. The output is the filename of the data encoded file.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe second call will provide the filename. The output is the numeric array by decoding the file.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eSuccess:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e File size less than 4,100,000 bytes and a correct decoded array.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eScoring:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Scaled file size.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eEncode Call:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eInput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e x % randi(2^32-1,1000)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eOutput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e filename\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e. .\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eDecode Call:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eInput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e filename\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eOutput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e x_array\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eNote: global and persistent are not allowed. Unexpected files will cause a Virus Detect Failure.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":810,"title":"Hamming Weight - Size Scoring","description":"The Hamming Weight, \u003chttp://en.wikipedia.org/wiki/Hamming_weight wiki Hamming Weight\u003e, in its most simple form is the number of ones in the binary representation of a value.\r\n\r\nThe task here is to create a fast Hamming Weight function/method such that processing many 4K x 4K images of 32 bit integer can be evaluated rapidly. Saw this question posed on Stack Overflow.\r\n\r\n*Input:* Vector of length N of 32 bit integer values.\r\n\r\n*Output:* Total number of ones of the binary representation\r\n\r\n*Scoring:* Normal Cody Size, while solving multiple cases without timing out\r\n\r\nExamples: Input [7 ; 3], output=[3 ; 2];  [16 32], output [1 ; 1]; [0 4294967295]  output [ 0 ; 32] FFFFFFFF Hex = 2^32-1\r\n\r\nStressing Test vector : uint32(randi(2^32,[4096*4096,1])-1)\r\n\r\n\r\nHelpful, possibly, global variables.\r\n\r\nb1=uint32(1431655765); b2=uint32(858993459); b3=uint32(252645135) b4=uint32(16711935); b5=uint32(65535);\r\n\r\nHex: b1=55555555 b2=33333333 b3=0F0F0F0F b4=00FF00FF b5=0000FFFF\r\n\r\nThe array num_ones is created for values 0-65535 (0:2^16-1). num_ones(1)=0, num_ones(2)=1, num_ones(3)=1,num_ones(4)=2,...num_ones(65536)=15\r\n\r\nDue to lack of zero indexing num_ones(value+1) is number of ones for value.","description_html":"\u003cp\u003eThe Hamming Weight, \u003ca href=\"http://en.wikipedia.org/wiki/Hamming_weight\"\u003ewiki Hamming Weight\u003c/a\u003e, in its most simple form is the number of ones in the binary representation of a value.\u003c/p\u003e\u003cp\u003eThe task here is to create a fast Hamming Weight function/method such that processing many 4K x 4K images of 32 bit integer can be evaluated rapidly. Saw this question posed on Stack Overflow.\u003c/p\u003e\u003cp\u003e\u003cb\u003eInput:\u003c/b\u003e Vector of length N of 32 bit integer values.\u003c/p\u003e\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e Total number of ones of the binary representation\u003c/p\u003e\u003cp\u003e\u003cb\u003eScoring:\u003c/b\u003e Normal Cody Size, while solving multiple cases without timing out\u003c/p\u003e\u003cp\u003eExamples: Input [7 ; 3], output=[3 ; 2];  [16 32], output [1 ; 1]; [0 4294967295]  output [ 0 ; 32] FFFFFFFF Hex = 2^32-1\u003c/p\u003e\u003cp\u003eStressing Test vector : uint32(randi(2^32,[4096*4096,1])-1)\u003c/p\u003e\u003cp\u003eHelpful, possibly, global variables.\u003c/p\u003e\u003cp\u003eb1=uint32(1431655765); b2=uint32(858993459); b3=uint32(252645135) b4=uint32(16711935); b5=uint32(65535);\u003c/p\u003e\u003cp\u003eHex: b1=55555555 b2=33333333 b3=0F0F0F0F b4=00FF00FF b5=0000FFFF\u003c/p\u003e\u003cp\u003eThe array num_ones is created for values 0-65535 (0:2^16-1). num_ones(1)=0, num_ones(2)=1, num_ones(3)=1,num_ones(4)=2,...num_ones(65536)=15\u003c/p\u003e\u003cp\u003eDue to lack of zero indexing num_ones(value+1) is number of ones for value.\u003c/p\u003e","function_template":"function y = Ham(x)\r\n% Input uint32\r\nglobal num_ones b1 b2 b3 b4 b5\r\n  y = x;\r\nend","test_suite":"%%\r\nglobal num_ones b1 b2 b3 b4 b5 net_time\r\nnet_time=4000; % default in case of time out (not needed)\r\n\r\nb1=uint32(1431655765); \r\nb2=uint32(858993459);\r\nb3=uint32(252645135);\r\nb4=uint32(16711935);\r\nb5=uint32(65535);\r\n \r\nnum_ones=uint32(zeros(65536,1)); \r\nfor i=0:65535  num_ones(i+1)=length( find( bitget( i, 1:32 ) ) ) ; \r\nend % Cody 0.996 sec\r\n%%\r\nglobal num_ones b1 b2 b3 b4 b5 net_time\r\n\r\nw=uint32(randi(2^32,[4096*1,1])-1); \r\nfor i=1:4 % Clear timing\r\n  vw=Ham(w);\r\nend\r\n\r\nwexpect=num_ones(mod(w,65536)+1)+num_ones(floor(double(w)/65536)+1); %1.56\r\nt0=clock;\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\ndt=etime(clock,t0)*250*1000; % avg of 4 runs in us\r\nfprintf('Time to execute 4096 values %.0f usec\\n',dt);\r\nassert(isequal(wexpect,vw),sprintf('\\nTime to execute 4096 values %.0f usec\\n',dt))\r\n%%\r\nglobal num_ones b1 b2 b3 b4 b5 net_time\r\n\r\nw=uint32(randi(2^32,[4096*1,1])-1); \r\n\r\n vw=Ham(w); % Three cycles of smaller vector\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n\r\nw=uint32(randi(2^32,[4096*4096,1])-1);\r\nwexpect=num_ones(mod(w,65536)+1)+num_ones(floor(double(w)/65536)+1); %1.56\r\n\r\n \r\n  vw=Ham(w); % Big Prep file\r\n  vw=Ham(w); % Big Prep file\r\n \r\nt0=clock;\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\nnet_time=etime(clock,t0)*250; % avg of 4 runs\r\nfprintf('Time to execute 4096*4096 values %.0f msec\\n',net_time);\r\n\r\nassert(isequal(wexpect,vw),sprintf('\\nTime to execute 4096*4096 values %.0f msec\\n',net_time))\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":0,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":10,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2012-07-01T02:47:44.000Z","updated_at":"2012-07-01T05:25:20.000Z","published_at":"2012-07-01T05:25:20.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe Hamming Weight,\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://en.wikipedia.org/wiki/Hamming_weight\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ewiki Hamming Weight\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e, in its most simple form is the number of ones in the binary representation of a value.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe task here is to create a fast Hamming Weight function/method such that processing many 4K x 4K images of 32 bit integer can be evaluated rapidly. Saw this question posed on Stack Overflow.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eInput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Vector of length N of 32 bit integer values.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eOutput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Total number of ones of the binary representation\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eScoring:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Normal Cody Size, while solving multiple cases without timing out\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExamples: Input [7 ; 3], output=[3 ; 2]; [16 32], output [1 ; 1]; [0 4294967295] output [ 0 ; 32] FFFFFFFF Hex = 2^32-1\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eStressing Test vector : uint32(randi(2^32,[4096*4096,1])-1)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHelpful, possibly, global variables.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eb1=uint32(1431655765); b2=uint32(858993459); b3=uint32(252645135) b4=uint32(16711935); b5=uint32(65535);\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHex: b1=55555555 b2=33333333 b3=0F0F0F0F b4=00FF00FF b5=0000FFFF\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe array num_ones is created for values 0-65535 (0:2^16-1). num_ones(1)=0, num_ones(2)=1, num_ones(3)=1,num_ones(4)=2,...num_ones(65536)=15\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eDue to lack of zero indexing num_ones(value+1) is number of ones for value.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"}],"problem_search":{"errors":[],"problems":[{"id":955,"title":"ismember: Enhanced Time Performance for 'rows'  - Speed Scoring (90% savings)","description":"The Challenge is to perform very fast the 'ismember' function for a long and wide array.\r\n\r\nThe data is small integer representing data permutations of items like DNA and Rubik's cube faces and orientations.\r\n\r\n*Input:* Array of uint8 of dimensions (m, 16) with values 0:3\r\n\r\n*Output:* Array Equivalent to ismember(A,B,'rows')\r\n\r\n*Hints:*\r\n\r\n1) Columns can be merged to form a reduced number of columns\r\n2) Unique has the option to provide an Array and a sorting Index","description_html":"\u003cp\u003eThe Challenge is to perform very fast the 'ismember' function for a long and wide array.\u003c/p\u003e\u003cp\u003eThe data is small integer representing data permutations of items like DNA and Rubik's cube faces and orientations.\u003c/p\u003e\u003cp\u003e\u003cb\u003eInput:\u003c/b\u003e Array of uint8 of dimensions (m, 16) with values 0:3\u003c/p\u003e\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e Array Equivalent to ismember(A,B,'rows')\u003c/p\u003e\u003cp\u003e\u003cb\u003eHints:\u003c/b\u003e\u003c/p\u003e\u003cp\u003e1) Columns can be merged to form a reduced number of columns\r\n2) Unique has the option to provide an Array and a sorting Index\u003c/p\u003e","function_template":"function idx = ismember_fast_rows(a,b)\r\n idx=ismember(a,b,'rows');\r\nend","test_suite":"%%\r\nfeval(@assignin,'caller','score',40000);\r\n%%\r\n% Functionality Tests\r\nL=128;\r\na=randi(4,L,16,'uint8')-1;\r\nb=randi(4,L,16,'uint8')-1;\r\nassert(isequal(ismember(a,b,'rows'),ismember_fast_rows(a,b)))\r\n\r\nb=a;\r\nassert(isequal(ismember(a,b,'rows'),ismember_fast_rows(a,b)))\r\n\r\nL=256;\r\na=randi(4,L,16,'uint8')-1;\r\nb=randi(4,L,16,'uint8')-1;\r\na(16:32,:)=b(32:48,:);\r\nassert(isequal(ismember(a,b,'rows'),ismember_fast_rows(a,b)))\r\n\r\n%%\r\nL=4000000;  % ismember 40    fast 5.2\r\n% 34 sec 4M\r\ntic\r\na=randi(4,L,16,'uint8')-1;\r\nb=randi(4,L,16,'uint8')-1;\r\ntoc\r\n\r\n\r\nta=clock;\r\nidx = ismember_fast_rows(a,b);\r\nt1=etime(clock,ta)*1000;\r\n\r\nfprintf('Elapsed time = %.0f msec\\n',t1)\r\n\r\n%assert(isequal(ismember(a,b,'rows'),idx))\r\n\r\nt2=min(40000,t1); % ismember scores 40000 msec\r\nfeval(@assignin,'caller','score',floor(t2));\r\n","published":true,"deleted":false,"likes_count":0,"comments_count":6,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":19,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2012-09-24T02:05:42.000Z","updated_at":"2025-06-08T09:02:05.000Z","published_at":"2012-09-24T05:39:06.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe Challenge is to perform very fast the 'ismember' function for a long and wide array.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe data is small integer representing data permutations of items like DNA and Rubik's cube faces and orientations.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eInput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Array of uint8 of dimensions (m, 16) with values 0:3\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eOutput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Array Equivalent to ismember(A,B,'rows')\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eHints:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e1) Columns can be merged to form a reduced number of columns 2) Unique has the option to provide an Array and a sorting Index\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":1789,"title":"Adding and Subtracting UINT variables","description":"Given UINT class variables output the correct solution to A-B+C.\r\n\r\n*Input:* A,B,C  Three uint variables \r\n\r\n*Output:* solution to A-B+C \r\n\r\n*Note:* Negative answer results in an expected Zero for class uint","description_html":"\u003cp\u003eGiven UINT class variables output the correct solution to A-B+C.\u003c/p\u003e\u003cp\u003e\u003cb\u003eInput:\u003c/b\u003e A,B,C  Three uint variables\u003c/p\u003e\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e solution to A-B+C\u003c/p\u003e\u003cp\u003e\u003cb\u003eNote:\u003c/b\u003e Negative answer results in an expected Zero for class uint\u003c/p\u003e","function_template":"function s=uint_function(A,B,C)\r\n  s=A-B+C;\r\nend","test_suite":"%%\r\nA=uint32(5);\r\nB=uint32(6);\r\nC=uint32(4);\r\ns_exp=3;\r\ns=uint_function(A,B,C);\r\nassert(isequal(s,s_exp),sprintf('s = %3i',s))\r\n%%\r\nA=uint32(7);\r\nB=uint32(6);\r\nC=uint32(12);\r\ns_exp=13;\r\ns=uint_function(A,B,C);\r\nassert(isequal(s,s_exp),sprintf('s = %3i',s))\r\n%%\r\nA=uint16(1);\r\nB=uint16(6);\r\nC=uint16(5);\r\ns_exp=0;\r\ns=uint_function(A,B,C);\r\nassert(isequal(s,s_exp),sprintf('s = %3i',s))\r\n%%\r\nA=uint64(12);\r\nB=uint64(14);\r\nC=uint64(3);\r\ns_exp=1;\r\ns=uint_function(A,B,C);\r\nassert(isequal(s,s_exp),sprintf('s = %3i',s))\r\n%%\r\nA=uint8(4);\r\nB=uint8(8);\r\nC=uint8(4);\r\ns_exp=0;\r\ns=uint_function(A,B,C);\r\nassert(isequal(s,s_exp),sprintf('s = %3i',s))\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":1,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":48,"test_suite_updated_at":"2013-08-12T19:33:57.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2013-08-12T19:19:02.000Z","updated_at":"2026-03-06T12:18:48.000Z","published_at":"2013-08-12T19:33:57.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eGiven UINT class variables output the correct solution to A-B+C.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eInput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e A,B,C Three uint variables\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eOutput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e solution to A-B+C\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eNote:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Negative answer results in an expected Zero for class uint\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":808,"title":"Hamming Weight - Fast","description":"The Hamming Weight, \u003chttp://en.wikipedia.org/wiki/Hamming_weight wiki Hamming Weight\u003e, in its most simple form is the number of ones in the binary representation of a value.\r\n\r\nThe task here is to create a fast Hamming Weight function/method such that processing many 4K x 4K images of 32 bit integer can be evaluated rapidly. Saw this question posed on Stack Overflow.\r\n\r\n*Input:* Vector of length N of 32 bit integer values.\r\n\r\n*Output:* Vector of number of ones of the binary representation\r\n\r\n*Scoring:* Time in milliseconds to process a [4096*4096,1] vector\r\n\r\n*Examples:* Input [7 ; 3], output=[3;2];  [16 32], output [1;1]; [0 4294967295] output [0;32]\r\n\r\n*Timing Test vector:* uint32(randi(2^32,[4096*4096,1])-1)\r\n\r\n*Minimum vector length/increment:* 65536\r\n\r\nHelpful, possibly, global variables.\r\n\r\nb1=uint32(1431655765); b2=uint32(858993459); b3=uint32(252645135) b4=uint32(16711935); b5=uint32(65535);\r\n\r\nHex: b1=55555555 b2=33333333 b3=0F0F0F0F b4=00FF00FF b5=0000FFFF \r\n\r\nThe array num_ones is created for values 0-65535 (0:2^16-1).\r\nnum_ones(1)=0, num_ones(2)=1, num_ones(3)=1,num_ones(4)=2,...num_ones(65536)=15\r\n\r\nDue to lack of zero indexing num_ones(value+1) is number of ones for value.\r\n\r\n\r\nHint: Globals are not good for time performance.\r\n\r\nHint: Segmentation appears to provide significant time optimization potential.\r\n\r\n\r\n\r\n","description_html":"\u003cp\u003eThe Hamming Weight, \u003ca href=\"http://en.wikipedia.org/wiki/Hamming_weight\"\u003ewiki Hamming Weight\u003c/a\u003e, in its most simple form is the number of ones in the binary representation of a value.\u003c/p\u003e\u003cp\u003eThe task here is to create a fast Hamming Weight function/method such that processing many 4K x 4K images of 32 bit integer can be evaluated rapidly. Saw this question posed on Stack Overflow.\u003c/p\u003e\u003cp\u003e\u003cb\u003eInput:\u003c/b\u003e Vector of length N of 32 bit integer values.\u003c/p\u003e\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e Vector of number of ones of the binary representation\u003c/p\u003e\u003cp\u003e\u003cb\u003eScoring:\u003c/b\u003e Time in milliseconds to process a [4096*4096,1] vector\u003c/p\u003e\u003cp\u003e\u003cb\u003eExamples:\u003c/b\u003e Input [7 ; 3], output=[3;2];  [16 32], output [1;1]; [0 4294967295] output [0;32]\u003c/p\u003e\u003cp\u003e\u003cb\u003eTiming Test vector:\u003c/b\u003e uint32(randi(2^32,[4096*4096,1])-1)\u003c/p\u003e\u003cp\u003e\u003cb\u003eMinimum vector length/increment:\u003c/b\u003e 65536\u003c/p\u003e\u003cp\u003eHelpful, possibly, global variables.\u003c/p\u003e\u003cp\u003eb1=uint32(1431655765); b2=uint32(858993459); b3=uint32(252645135) b4=uint32(16711935); b5=uint32(65535);\u003c/p\u003e\u003cp\u003eHex: b1=55555555 b2=33333333 b3=0F0F0F0F b4=00FF00FF b5=0000FFFF\u003c/p\u003e\u003cp\u003eThe array num_ones is created for values 0-65535 (0:2^16-1).\r\nnum_ones(1)=0, num_ones(2)=1, num_ones(3)=1,num_ones(4)=2,...num_ones(65536)=15\u003c/p\u003e\u003cp\u003eDue to lack of zero indexing num_ones(value+1) is number of ones for value.\u003c/p\u003e\u003cp\u003eHint: Globals are not good for time performance.\u003c/p\u003e\u003cp\u003eHint: Segmentation appears to provide significant time optimization potential.\u003c/p\u003e","function_template":"function y = Ham(x)\r\n% Input uint32\r\nglobal num_ones b1 b2 b3 b4 b5\r\n  y = x;\r\nend","test_suite":"%%\r\nfeval(@assignin,'caller','score',2000);\r\n%%\r\nglobal num_ones b1 b2 b3 b4 b5 net_time\r\nnet_time=2000; % default in case of time out (not needed)\r\n\r\nb1=uint32(1431655765); \r\nb2=uint32(858993459);\r\nb3=uint32(252645135);\r\nb4=uint32(16711935);\r\nb5=uint32(65535);\r\n \r\nnum_ones=uint32(zeros(65536,1)); \r\nfor i=0:65535  num_ones(i+1)=length( find( bitget( i, 1:32 ) ) ) ; \r\nend % Cody 0.996 sec\r\n%%\r\nglobal num_ones b1 b2 b3 b4 b5 net_time\r\n\r\nw=uint32(randi(2^32,[65536*1,1])-1); \r\nfor i=1:4 % Clear timing\r\n  vw=Ham(w);\r\nend\r\n\r\nwexpect=num_ones(mod(w,65536)+1)+num_ones(floor(double(w)/65536)+1); %1.56\r\nt0=clock;\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\ndt=etime(clock,t0)*250*1000; % avg of 4 runs in us\r\nfprintf('Time to execute 65536 values %.0f usec\\n',dt);\r\nassert(isequal(wexpect,vw),sprintf('Time to execute 65536 values %.0f usec\\n',dt))\r\n%%\r\nglobal num_ones b1 b2 b3 b4 b5 net_time\r\n\r\nw=uint32(randi(2^32,[65536*1,1])-1); \r\n\r\n vw=Ham(w); % Three cycles of smaller vector\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n\r\nw=uint32(randi(2^32,[4096*4096,1])-1);\r\nwexpect=num_ones(mod(w,65536)+1)+num_ones(floor(double(w)/65536)+1); %1.56\r\n\r\n \r\n  vw=Ham(w); % Big Prep file\r\n  vw=Ham(w); % Big Prep file\r\n \r\nt0=clock;\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\nnet_time=etime(clock,t0)*250; % avg of 4 runs\r\nfprintf('Time to execute 4096*4096 values %.0f msec\\n',net_time);\r\n\r\nassert(isequal(wexpect,vw),sprintf('Time to execute 4096*4096 values %.0f msec\\n',net_time))\r\n%%\r\nglobal net_time\r\n% net_time in ms\r\n% Create graph data\r\nnet_time=min(2000,net_time); % Limit graph y-axis\r\n\r\nfeval(@assignin,'caller','score',floor(net_time));\r\n\r\n%fh=fopen('Ham.m','wt');\r\n%fprintf(fh,'%s\\n',repmat('1;',[1,round(net_time/2)]));\r\n%fclose(fh);","published":true,"deleted":false,"likes_count":0,"comments_count":1,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":18,"test_suite_updated_at":"2012-11-22T11:18:57.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2012-06-30T05:08:08.000Z","updated_at":"2026-02-09T12:51:11.000Z","published_at":"2012-07-01T05:19:12.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe Hamming Weight,\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://en.wikipedia.org/wiki/Hamming_weight\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ewiki Hamming Weight\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e, in its most simple form is the number of ones in the binary representation of a value.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe task here is to create a fast Hamming Weight function/method such that processing many 4K x 4K images of 32 bit integer can be evaluated rapidly. Saw this question posed on Stack Overflow.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eInput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Vector of length N of 32 bit integer values.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eOutput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Vector of number of ones of the binary representation\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eScoring:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Time in milliseconds to process a [4096*4096,1] vector\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eExamples:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Input [7 ; 3], output=[3;2]; [16 32], output [1;1]; [0 4294967295] output [0;32]\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eTiming Test vector:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e uint32(randi(2^32,[4096*4096,1])-1)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eMinimum vector length/increment:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e 65536\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHelpful, possibly, global variables.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eb1=uint32(1431655765); b2=uint32(858993459); b3=uint32(252645135) b4=uint32(16711935); b5=uint32(65535);\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHex: b1=55555555 b2=33333333 b3=0F0F0F0F b4=00FF00FF b5=0000FFFF\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe array num_ones is created for values 0-65535 (0:2^16-1). num_ones(1)=0, num_ones(2)=1, num_ones(3)=1,num_ones(4)=2,...num_ones(65536)=15\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eDue to lack of zero indexing num_ones(value+1) is number of ones for value.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHint: Globals are not good for time performance.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHint: Segmentation appears to provide significant time optimization potential.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":1148,"title":"Compression: Encode and Decode","description":"This Challenge is to compress an array created by randi(2^32-1,1000) and then decompress your data file.\r\n\r\nThe first call will provide the array x. The output is the filename of the data encoded file. \r\n\r\nThe second call will provide the filename. The output is the numeric array by decoding the file.\r\n\r\n\r\n\r\n*Success:* File size less than 4,100,000 bytes and a correct decoded array.\r\n\r\n*Scoring:* Scaled file size.\r\n\r\n*Encode Call:*\r\n\r\n*Input:* x % randi(2^32-1,1000)\r\n\r\n*Output:* filename\r\n\r\n.\r\n.\r\n\r\n*Decode Call:*\r\n\r\n*Input:* filename\r\n\r\n*Output:* x_array\r\n  \r\nNote: global and persistent are not allowed. Unexpected files will cause a Virus Detect Failure.","description_html":"\u003cp\u003eThis Challenge is to compress an array created by randi(2^32-1,1000) and then decompress your data file.\u003c/p\u003e\u003cp\u003eThe first call will provide the array x. The output is the filename of the data encoded file.\u003c/p\u003e\u003cp\u003eThe second call will provide the filename. The output is the numeric array by decoding the file.\u003c/p\u003e\u003cp\u003e\u003cb\u003eSuccess:\u003c/b\u003e File size less than 4,100,000 bytes and a correct decoded array.\u003c/p\u003e\u003cp\u003e\u003cb\u003eScoring:\u003c/b\u003e Scaled file size.\u003c/p\u003e\u003cp\u003e\u003cb\u003eEncode Call:\u003c/b\u003e\u003c/p\u003e\u003cp\u003e\u003cb\u003eInput:\u003c/b\u003e x % randi(2^32-1,1000)\u003c/p\u003e\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e filename\u003c/p\u003e\u003cp\u003e.\r\n.\u003c/p\u003e\u003cp\u003e\u003cb\u003eDecode Call:\u003c/b\u003e\u003c/p\u003e\u003cp\u003e\u003cb\u003eInput:\u003c/b\u003e filename\u003c/p\u003e\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e x_array\u003c/p\u003e\u003cp\u003eNote: global and persistent are not allowed. Unexpected files will cause a Virus Detect Failure.\u003c/p\u003e","function_template":"function P = encode_decode(x)\r\n% P is the file_name string if x is an array\r\n% P is an array if x is the filename to decode\r\n P=x;\r\nend","test_suite":"%%\r\nfeval(@assignin,'caller','score',8000);\r\n%%\r\nfiletext = fileread('encode_decode.m');\r\n% Avoid Size 0 solutions\r\nassert(isempty(strfind(filetext, 'persistent')))\r\nassert(isempty(strfind(filetext, 'global')))\r\n%%\r\nx=randi(2^32-1,1000);\r\n\r\n\r\ndirfiles_orig=dir;\r\nnum_orig=size(dirfiles_orig,1);\r\n\r\nfname=encode_decode(x);\r\n\r\ndirfiles=dir;\r\nnum_encode=size(dirfiles,1);\r\n\r\nc=dir(fname);\r\nfprintf('Bytes %i\\n',c.bytes)\r\nfile_size=floor(c.bytes/1000);\r\n\r\nassert(num_encode==num_orig+1,'Virus Detected');\r\n\r\n\r\nxout=encode_decode(fname);\r\n\r\n\r\nassert(file_size\u003c4100,sprintf('File Size %i\\n',file_size)) \r\nassert(isequal(x,xout),'xout ~= x')\r\n\r\nfeval(  @assignin,'caller','score',floor(min( 8000,file_size ))  );\r\n\r\n\r\n","published":true,"deleted":false,"likes_count":2,"comments_count":0,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":12,"test_suite_updated_at":"2012-12-30T03:12:39.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2012-12-28T22:43:52.000Z","updated_at":"2012-12-30T03:21:48.000Z","published_at":"2012-12-28T23:29:43.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThis Challenge is to compress an array created by randi(2^32-1,1000) and then decompress your data file.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe first call will provide the array x. The output is the filename of the data encoded file.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe second call will provide the filename. The output is the numeric array by decoding the file.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eSuccess:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e File size less than 4,100,000 bytes and a correct decoded array.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eScoring:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Scaled file size.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eEncode Call:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eInput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e x % randi(2^32-1,1000)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eOutput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e filename\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e. .\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eDecode Call:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eInput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e filename\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eOutput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e x_array\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eNote: global and persistent are not allowed. Unexpected files will cause a Virus Detect Failure.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":810,"title":"Hamming Weight - Size Scoring","description":"The Hamming Weight, \u003chttp://en.wikipedia.org/wiki/Hamming_weight wiki Hamming Weight\u003e, in its most simple form is the number of ones in the binary representation of a value.\r\n\r\nThe task here is to create a fast Hamming Weight function/method such that processing many 4K x 4K images of 32 bit integer can be evaluated rapidly. Saw this question posed on Stack Overflow.\r\n\r\n*Input:* Vector of length N of 32 bit integer values.\r\n\r\n*Output:* Total number of ones of the binary representation\r\n\r\n*Scoring:* Normal Cody Size, while solving multiple cases without timing out\r\n\r\nExamples: Input [7 ; 3], output=[3 ; 2];  [16 32], output [1 ; 1]; [0 4294967295]  output [ 0 ; 32] FFFFFFFF Hex = 2^32-1\r\n\r\nStressing Test vector : uint32(randi(2^32,[4096*4096,1])-1)\r\n\r\n\r\nHelpful, possibly, global variables.\r\n\r\nb1=uint32(1431655765); b2=uint32(858993459); b3=uint32(252645135) b4=uint32(16711935); b5=uint32(65535);\r\n\r\nHex: b1=55555555 b2=33333333 b3=0F0F0F0F b4=00FF00FF b5=0000FFFF\r\n\r\nThe array num_ones is created for values 0-65535 (0:2^16-1). num_ones(1)=0, num_ones(2)=1, num_ones(3)=1,num_ones(4)=2,...num_ones(65536)=15\r\n\r\nDue to lack of zero indexing num_ones(value+1) is number of ones for value.","description_html":"\u003cp\u003eThe Hamming Weight, \u003ca href=\"http://en.wikipedia.org/wiki/Hamming_weight\"\u003ewiki Hamming Weight\u003c/a\u003e, in its most simple form is the number of ones in the binary representation of a value.\u003c/p\u003e\u003cp\u003eThe task here is to create a fast Hamming Weight function/method such that processing many 4K x 4K images of 32 bit integer can be evaluated rapidly. Saw this question posed on Stack Overflow.\u003c/p\u003e\u003cp\u003e\u003cb\u003eInput:\u003c/b\u003e Vector of length N of 32 bit integer values.\u003c/p\u003e\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e Total number of ones of the binary representation\u003c/p\u003e\u003cp\u003e\u003cb\u003eScoring:\u003c/b\u003e Normal Cody Size, while solving multiple cases without timing out\u003c/p\u003e\u003cp\u003eExamples: Input [7 ; 3], output=[3 ; 2];  [16 32], output [1 ; 1]; [0 4294967295]  output [ 0 ; 32] FFFFFFFF Hex = 2^32-1\u003c/p\u003e\u003cp\u003eStressing Test vector : uint32(randi(2^32,[4096*4096,1])-1)\u003c/p\u003e\u003cp\u003eHelpful, possibly, global variables.\u003c/p\u003e\u003cp\u003eb1=uint32(1431655765); b2=uint32(858993459); b3=uint32(252645135) b4=uint32(16711935); b5=uint32(65535);\u003c/p\u003e\u003cp\u003eHex: b1=55555555 b2=33333333 b3=0F0F0F0F b4=00FF00FF b5=0000FFFF\u003c/p\u003e\u003cp\u003eThe array num_ones is created for values 0-65535 (0:2^16-1). num_ones(1)=0, num_ones(2)=1, num_ones(3)=1,num_ones(4)=2,...num_ones(65536)=15\u003c/p\u003e\u003cp\u003eDue to lack of zero indexing num_ones(value+1) is number of ones for value.\u003c/p\u003e","function_template":"function y = Ham(x)\r\n% Input uint32\r\nglobal num_ones b1 b2 b3 b4 b5\r\n  y = x;\r\nend","test_suite":"%%\r\nglobal num_ones b1 b2 b3 b4 b5 net_time\r\nnet_time=4000; % default in case of time out (not needed)\r\n\r\nb1=uint32(1431655765); \r\nb2=uint32(858993459);\r\nb3=uint32(252645135);\r\nb4=uint32(16711935);\r\nb5=uint32(65535);\r\n \r\nnum_ones=uint32(zeros(65536,1)); \r\nfor i=0:65535  num_ones(i+1)=length( find( bitget( i, 1:32 ) ) ) ; \r\nend % Cody 0.996 sec\r\n%%\r\nglobal num_ones b1 b2 b3 b4 b5 net_time\r\n\r\nw=uint32(randi(2^32,[4096*1,1])-1); \r\nfor i=1:4 % Clear timing\r\n  vw=Ham(w);\r\nend\r\n\r\nwexpect=num_ones(mod(w,65536)+1)+num_ones(floor(double(w)/65536)+1); %1.56\r\nt0=clock;\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\ndt=etime(clock,t0)*250*1000; % avg of 4 runs in us\r\nfprintf('Time to execute 4096 values %.0f usec\\n',dt);\r\nassert(isequal(wexpect,vw),sprintf('\\nTime to execute 4096 values %.0f usec\\n',dt))\r\n%%\r\nglobal num_ones b1 b2 b3 b4 b5 net_time\r\n\r\nw=uint32(randi(2^32,[4096*1,1])-1); \r\n\r\n vw=Ham(w); % Three cycles of smaller vector\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n\r\nw=uint32(randi(2^32,[4096*4096,1])-1);\r\nwexpect=num_ones(mod(w,65536)+1)+num_ones(floor(double(w)/65536)+1); %1.56\r\n\r\n \r\n  vw=Ham(w); % Big Prep file\r\n  vw=Ham(w); % Big Prep file\r\n \r\nt0=clock;\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\nnet_time=etime(clock,t0)*250; % avg of 4 runs\r\nfprintf('Time to execute 4096*4096 values %.0f msec\\n',net_time);\r\n\r\nassert(isequal(wexpect,vw),sprintf('\\nTime to execute 4096*4096 values %.0f msec\\n',net_time))\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":0,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":10,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2012-07-01T02:47:44.000Z","updated_at":"2012-07-01T05:25:20.000Z","published_at":"2012-07-01T05:25:20.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe Hamming Weight,\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://en.wikipedia.org/wiki/Hamming_weight\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ewiki Hamming Weight\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e, in its most simple form is the number of ones in the binary representation of a value.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe task here is to create a fast Hamming Weight function/method such that processing many 4K x 4K images of 32 bit integer can be evaluated rapidly. Saw this question posed on Stack Overflow.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eInput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Vector of length N of 32 bit integer values.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eOutput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Total number of ones of the binary representation\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eScoring:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Normal Cody Size, while solving multiple cases without timing out\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExamples: Input [7 ; 3], output=[3 ; 2]; [16 32], output [1 ; 1]; [0 4294967295] output [ 0 ; 32] FFFFFFFF Hex = 2^32-1\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eStressing Test vector : uint32(randi(2^32,[4096*4096,1])-1)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHelpful, possibly, global variables.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eb1=uint32(1431655765); b2=uint32(858993459); b3=uint32(252645135) b4=uint32(16711935); b5=uint32(65535);\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHex: b1=55555555 b2=33333333 b3=0F0F0F0F b4=00FF00FF b5=0000FFFF\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe array num_ones is created for values 0-65535 (0:2^16-1). num_ones(1)=0, num_ones(2)=1, num_ones(3)=1,num_ones(4)=2,...num_ones(65536)=15\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eDue to lack of zero indexing num_ones(value+1) is number of ones for value.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"}],"term":"tag:\"uint32\"","current_player_id":null,"fields":[{"name":"page","type":"integer","callback":null,"default":1,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"per_page","type":"integer","callback":null,"default":50,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"sort","type":"string","callback":null,"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"body","type":"text","callback":null,"default":"*:*","directive":null,"facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":false},{"name":"group","type":"string","callback":null,"default":null,"directive":"group","facet":true,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"difficulty_rating_bin","type":"string","callback":null,"default":null,"directive":"difficulty_rating_bin","facet":true,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"id","type":"integer","callback":null,"default":null,"directive":"id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"tag","type":"string","callback":null,"default":null,"directive":"tag","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"product","type":"string","callback":null,"default":null,"directive":"product","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"created_at","type":"timeframe","callback":{},"default":null,"directive":"created_at","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"profile_id","type":"integer","callback":null,"default":null,"directive":"author_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"created_by","type":"string","callback":null,"default":null,"directive":"author","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"player_id","type":"integer","callback":null,"default":null,"directive":"solver_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"player","type":"string","callback":null,"default":null,"directive":"solver","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"solvers_count","type":"integer","callback":null,"default":null,"directive":"solvers_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"comments_count","type":"integer","callback":null,"default":null,"directive":"comments_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"likes_count","type":"integer","callback":null,"default":null,"directive":"likes_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"leader_id","type":"integer","callback":null,"default":null,"directive":"leader_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"leading_solution","type":"integer","callback":null,"default":null,"directive":"leading_solution","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true}],"filters":[{"name":"asset_type","type":"string","callback":null,"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":"\"cody:problem\"","prepend":true},{"name":"profile_id","type":"integer","callback":{},"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":"author_id","static":null,"prepend":true}],"query":{"params":{"per_page":50,"term":"tag:\"uint32\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"uint32\"","","\"","uint32","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007f0817676ff8\u003e":null,"#\u003cMathWorks::Search::Field:0x00007f0817676f58\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007f0817676698\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007f0817677278\u003e":1,"#\u003cMathWorks::Search::Field:0x00007f08176771d8\u003e":50,"#\u003cMathWorks::Search::Field:0x00007f0817677138\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007f0817677098\u003e":"tag:\"uint32\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007f0817677098\u003e":"tag:\"uint32\""},"queried_facets":{}},"query_backend":{"connection":{"configuration":{"index_url":"http://index-op-v2/solr/","query_url":"http://search-op-v2/solr/","direct_access_index_urls":["http://index-op-v2/solr/"],"direct_access_query_urls":["http://search-op-v2/solr/"],"timeout":10,"vhost":"search","exchange":"search.topic","heartbeat":30,"pre_index_mode":false,"host":"rabbitmq-eks","port":5672,"username":"cody-search","password":"78X075ddcV44","virtual_host":"search","indexer":"amqp","http_logging":"true","core":"cody"},"query_connection":{"uri":"http://search-op-v2/solr/cody/","proxy":null,"connection":{"parallel_manager":null,"headers":{"User-Agent":"Faraday v1.0.1"},"params":{},"options":{"params_encoder":"Faraday::FlatParamsEncoder","proxy":null,"bind":null,"timeout":null,"open_timeout":null,"read_timeout":null,"write_timeout":null,"boundary":null,"oauth":null,"context":null,"on_data":null},"ssl":{"verify":true,"ca_file":null,"ca_path":null,"verify_mode":null,"cert_store":null,"client_cert":null,"client_key":null,"certificate":null,"private_key":null,"verify_depth":null,"version":null,"min_version":null,"max_version":null},"default_parallel_manager":null,"builder":{"adapter":{"name":"Faraday::Adapter::NetHttp","args":[],"block":null},"handlers":[{"name":"Faraday::Response::RaiseError","args":[],"block":null}],"app":{"app":{"ssl_cert_store":{"verify_callback":null,"error":null,"error_string":null,"chain":null,"time":null},"app":{},"connection_options":{},"config_block":null}}},"url_prefix":"http://search-op-v2/solr/cody/","manual_proxy":false,"proxy":null},"update_format":"RSolr::JSON::Generator","update_path":"update","options":{"url":"http://search-op-v2/solr/cody"}}},"query":{"params":{"per_page":50,"term":"tag:\"uint32\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"uint32\"","","\"","uint32","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007f0817676ff8\u003e":null,"#\u003cMathWorks::Search::Field:0x00007f0817676f58\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007f0817676698\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007f0817677278\u003e":1,"#\u003cMathWorks::Search::Field:0x00007f08176771d8\u003e":50,"#\u003cMathWorks::Search::Field:0x00007f0817677138\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007f0817677098\u003e":"tag:\"uint32\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007f0817677098\u003e":"tag:\"uint32\""},"queried_facets":{}},"options":{"fields":["id","difficulty_rating"]},"join":" "},"results":[{"id":955,"difficulty_rating":"easy"},{"id":1789,"difficulty_rating":"easy"},{"id":808,"difficulty_rating":"easy"},{"id":1148,"difficulty_rating":"easy-medium"},{"id":810,"difficulty_rating":"medium"}]}}