{"group":{"id":1,"name":"Community","lockable":false,"created_at":"2012-01-18T18:02:15.000Z","updated_at":"2025-12-14T01:33:56.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":"2025-12-14T00: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":411,"title":"Back to basics 21 - Matrix replicating","description":"Covering some basic topics I haven't seen elsewhere on Cody.\r\n\r\nGiven an input matrix, generate an output matrix that consists of the input matrix replicated in a 2 by 2 pattern Examples:\r\n\r\n  [1] -\u003e [1 1\r\n          1 1]\r\n\r\n  [1 2       -\u003e [1 2 1 2\r\n   3 4]          3 4 3 4 \r\n                 1 2 1 2 \r\n                 3 4 3 4]","description_html":"\u003cp\u003eCovering some basic topics I haven't seen elsewhere on Cody.\u003c/p\u003e\u003cp\u003eGiven an input matrix, generate an output matrix that consists of the input matrix replicated in a 2 by 2 pattern Examples:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e[1] -\u003e [1 1\r\n        1 1]\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003e[1 2       -\u003e [1 2 1 2\r\n 3 4]          3 4 3 4 \r\n               1 2 1 2 \r\n               3 4 3 4]\r\n\u003c/pre\u003e","function_template":"function y = matrix_replication(x)\r\n  y = x;\r\nend","test_suite":"%%\r\nx = [1];\r\ny_correct = [1 1;1 1];\r\nassert(isequal(matrix_replication(x),y_correct))\r\n\r\n%%\r\nx = [1 2;3 4];\r\ny_correct = [1 2 1 2; 3 4 3 4; 1 2 1 2; 3 4 3 4];\r\nassert(isequal(matrix_replication(x),y_correct))\r\n\r\n\r\n%%\r\nx = [1 2];\r\ny_correct = [1 2 1 2; 1 2 1 2];\r\nassert(isequal(matrix_replication(x),y_correct))","published":true,"deleted":false,"likes_count":8,"comments_count":0,"created_by":1022,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":1798,"test_suite_updated_at":"2012-02-25T21:33:19.000Z","rescore_all_solutions":false,"group_id":19,"created_at":"2012-02-25T21:33:19.000Z","updated_at":"2026-03-28T23:57:05.000Z","published_at":"2012-02-25T21:33:19.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\u003eCovering some basic topics I haven't seen elsewhere on Cody.\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\u003eGiven an input matrix, generate an output matrix that consists of the input matrix replicated in a 2 by 2 pattern Examples:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[[1] -\u003e [1 1\\n        1 1]\\n\\n[1 2       -\u003e [1 2 1 2\\n 3 4]          3 4 3 4 \\n               1 2 1 2 \\n               3 4 3 4]]]\u003e\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":413,"title":"Back to basics 23 - Triangular matrix","description":"Covering some basic topics I haven't seen elsewhere on Cody.\r\n\r\nGiven an input matrix, return a matrix with all elements above and to the right of the main diagonal set to zero. Example:\r\n\r\n  input = [ 1 1 1 1; 1 1 1 1; 1 1 1 1; 1 1 1 1]\r\n  output = [1 0 0 0; 1 1 0 0; 1 1 1 0; 1 1 1 1]\r\n","description_html":"\u003cp\u003eCovering some basic topics I haven't seen elsewhere on Cody.\u003c/p\u003e\u003cp\u003eGiven an input matrix, return a matrix with all elements above and to the right of the main diagonal set to zero. Example:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003einput = [ 1 1 1 1; 1 1 1 1; 1 1 1 1; 1 1 1 1]\r\noutput = [1 0 0 0; 1 1 0 0; 1 1 1 0; 1 1 1 1]\r\n\u003c/pre\u003e","function_template":"function y = triangle(x)\r\n  y = x;\r\nend","test_suite":"%%\r\nx = ones(4,4);\r\ny_correct = [1 0 0 0; 1 1 0 0; 1 1 1 0; 1 1 1 1];\r\nassert(isequal(triangle(x),y_correct))\r\n\r\n%%\r\nx = ones(5,5);\r\ny_correct = [1 0 0 0 0; 1 1 0 0 0; 1 1 1 0 0; 1 1 1 1 0; 1 1 1 1 1];\r\nassert(isequal(triangle(x),y_correct))\r\n\r\n%%\r\nx = reshape(1:16,[4,4]);\r\ny_correct = [1 2 3 4; 0 6 7 8; 0 0 11 12; 0 0 0 16]';\r\nassert(isequal(triangle(x),y_correct))\r\n\r\n%%\r\nx = [0 1 2 3; 0 0 1 2; 0 0 0 1; 0 0 0 0];\r\ny_correct = zeros(4);\r\nassert(isequal(triangle(x),y_correct))","published":true,"deleted":false,"likes_count":7,"comments_count":3,"created_by":1022,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":1123,"test_suite_updated_at":"2017-02-24T16:49:14.000Z","rescore_all_solutions":false,"group_id":19,"created_at":"2012-02-25T21:46:25.000Z","updated_at":"2026-04-03T20:13:43.000Z","published_at":"2012-02-25T21:46:25.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\u003eCovering some basic topics I haven't seen elsewhere on Cody.\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\u003eGiven an input matrix, return a matrix with all elements above and to the right of the main diagonal set to zero. Example:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[input = [ 1 1 1 1; 1 1 1 1; 1 1 1 1; 1 1 1 1]\\noutput = [1 0 0 0; 1 1 0 0; 1 1 1 0; 1 1 1 1]]]\u003e\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":1129,"title":"Reverse the elements of an array","description":"Reverse the order of elements in an array:\r\n\r\neg:\r\n\r\n input X  = [ 1 2 3 ; \r\n              4 5 6 ; \r\n              7 8 9 ]\r\n\r\n output Y = [ 9 8 7 ;\r\n              6 5 4 ;\r\n              3 2 1 ]\r\n","description_html":"\u003cp\u003eReverse the order of elements in an array:\u003c/p\u003e\u003cp\u003eeg:\u003c/p\u003e\u003cpre\u003e input X  = [ 1 2 3 ; \r\n              4 5 6 ; \r\n              7 8 9 ]\u003c/pre\u003e\u003cpre\u003e output Y = [ 9 8 7 ;\r\n              6 5 4 ;\r\n              3 2 1 ]\u003c/pre\u003e","function_template":"function y = your_fcn_name(x)\r\n  y = x;\r\nend","test_suite":"%%\r\nx = [];\r\ny_correct = [];\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n\r\n%%\r\nx = 1;\r\ny_correct = 1;\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n\r\n%%\r\nx = [1:5];\r\ny_correct = [5:-1:1];\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n\r\n%%\r\nx = [1:5;6:10];\r\ny_correct = [10:-1:6;5:-1:1];\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n\r\n%%\r\nx = [1:5;6:10;11:15];\r\ny_correct = [15:-1:11;10:-1:6;5:-1:1];\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n\r\n%%\r\nx = ones(5);\r\ny_correct = ones(5);\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n\r\n%%\r\nx = magic(3);\r\ny_correct = [2 9 4;7 5 3;6 1 8];\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n\r\n%%\r\nx = [2 9 -4;7 -5 3;-6 1 8];\r\ny_correct =[8 1 -6;3 -5 7;-4 9 2] ;\r\nassert(isequal(your_fcn_name(x),y_correct))","published":true,"deleted":false,"likes_count":8,"comments_count":0,"created_by":9317,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":1115,"test_suite_updated_at":"2012-12-18T17:05:19.000Z","rescore_all_solutions":false,"group_id":19,"created_at":"2012-12-18T16:56:00.000Z","updated_at":"2026-03-25T11:14:35.000Z","published_at":"2012-12-18T16:57:22.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\u003eReverse the order of elements in an 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\u003eeg:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ input X  = [ 1 2 3 ; \\n              4 5 6 ; \\n              7 8 9 ]\\n\\n output Y = [ 9 8 7 ;\\n              6 5 4 ;\\n              3 2 1 ]]]\u003e\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":545,"title":"Flip the main diagonal of a matrix","description":"Given a n x n matrix, M, flip its main diagonal.\r\n\r\nExample:\r\n\r\n \u003e\u003e M=magic(5);\r\n\r\n \u003e\u003e flipDiagonal(M)\r\n\r\n     9    24     1     8    15\r\n    23    21     7    14    16\r\n     4     6    13    20    22\r\n    10    12    19     5     3\r\n    11    18    25     2    17\r\n","description_html":"\u003cp\u003eGiven a n x n matrix, M, flip its main diagonal.\u003c/p\u003e\u003cp\u003eExample:\u003c/p\u003e\u003cpre\u003e \u003e\u003e M=magic(5);\u003c/pre\u003e\u003cpre\u003e \u003e\u003e flipDiagonal(M)\u003c/pre\u003e\u003cpre\u003e     9    24     1     8    15\r\n    23    21     7    14    16\r\n     4     6    13    20    22\r\n    10    12    19     5     3\r\n    11    18    25     2    17\u003c/pre\u003e","function_template":"function y = flipDiagonal(x)\r\n  y = x\r\nend","test_suite":"%% test 1\r\nM=magic(5);\r\ny_correct = [9, 24,1,8 ,15; 23 ,21,7,14,16; ...\r\n    4,6,13,20,22; 10,12,19,5,3; 11,18,25,2,17];\r\nassert(isequal(flipDiagonal(M),y_correct))\r\n\r\n%% test 2\r\nM=magic(2);\r\ny_correct=[2 3; 4 1];\r\nassert(isequal(flipDiagonal(M),y_correct))\r\n\r\n%% \r\nM=ones(4);\r\nM(2,2)=4; M(3,3)=9; M(4,4)=16;\r\ny_correct=ones(4);\r\ny_correct(1,1)=16; y_correct(2,2)=9; y_correct(3,3)=4;\r\nassert(isequal(flipDiagonal(M),y_correct))\r\n\r\n%% \r\nM=[1];\r\ny_correct=[1];\r\nassert(isequal(flipDiagonal(M),y_correct))\r\n\r\n%% \r\nM=reshape(1:25,[5,5]);\r\ny_correct=[25 2 3 4 5; 6 19 8 9 10; ...\r\n    11 12 13 14 15; 16 17 18 7 20; 21 22 23 24 1]';\r\nassert(isequal(flipDiagonal(M),y_correct))\r\n\r\n%% \r\nM=[1,1,1,1;2,2,2,2;3,3,3,3;4,4,4,4];\r\ny_correct=[4,1,1,1;2,3,2,2;3,3,2,3;4,4,4,1];\r\nassert(isequal(flipDiagonal(M),y_correct))\r\n","published":true,"deleted":false,"likes_count":7,"comments_count":1,"created_by":1103,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":905,"test_suite_updated_at":"2017-02-24T16:41:36.000Z","rescore_all_solutions":false,"group_id":19,"created_at":"2012-04-01T03:31:45.000Z","updated_at":"2026-03-25T11:36:45.000Z","published_at":"2012-04-04T02:20:41.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 a n x n matrix, M, flip its main diagonal.\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\u003eExample:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ \u003e\u003e M=magic(5);\\n\\n \u003e\u003e flipDiagonal(M)\\n\\n     9    24     1     8    15\\n    23    21     7    14    16\\n     4     6    13    20    22\\n    10    12    19     5     3\\n    11    18    25     2    17]]\u003e\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":794,"title":"Matrix with different incremental runs","description":"Given a vector of positive integers\r\n\r\n  a = [ 3 2 4 ];\r\n\r\ncreate the matrix where the *i* th column contains the vector *1:a(i)* possibly padded\r\nwith zeros:\r\n\r\n  b = [ 1 1 1\r\n        2 2 2\r\n        3 0 3\r\n        0 0 4 ];","description_html":"\u003cp\u003eGiven a vector of positive integers\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003ea = [ 3 2 4 ];\r\n\u003c/pre\u003e\u003cp\u003ecreate the matrix where the \u003cb\u003ei\u003c/b\u003e th column contains the vector \u003cb\u003e1:a(i)\u003c/b\u003e possibly padded\r\nwith zeros:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eb = [ 1 1 1\r\n      2 2 2\r\n      3 0 3\r\n      0 0 4 ];\r\n\u003c/pre\u003e","function_template":"function b = your_fcn_name(a)\r\n  b = a;\r\nend","test_suite":"%%\r\na = [ 3 2 4 ];\r\nb = [ 1 1 1\r\n        2 2 2\r\n        3 0 3\r\n        0 0 4 ];\r\nassert(isequal(your_fcn_name(a),b))\r\n\r\n\r\n%%\r\na = [1 0 8 9 14];\r\nb =[ 1     0     1     1     1\r\n     0     0     2     2     2\r\n     0     0     3     3     3\r\n     0     0     4     4     4\r\n     0     0     5     5     5\r\n     0     0     6     6     6\r\n     0     0     7     7     7\r\n     0     0     8     8     8\r\n     0     0     0     9     9\r\n     0     0     0     0    10\r\n     0     0     0     0    11\r\n     0     0     0     0    12\r\n     0     0     0     0    13\r\n     0     0     0     0    14];\r\nassert(isequal(your_fcn_name(a),b))\r\n\r\n%%\r\n a = [ 4 2 4 ];\r\nb =[ 1     1     1\r\n     2     2     2\r\n     3     0     3\r\n     4     0     4];\r\nassert(isequal(your_fcn_name(a),b))\r\n\r\n%% one value\r\na= 1;\r\n\r\nassert(isequal(your_fcn_name(a),1))\r\n\r\n%% empty vector \r\na = [];\r\n\r\nassert(isequal(your_fcn_name(a),[]))","published":true,"deleted":false,"likes_count":5,"comments_count":0,"created_by":639,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":581,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":19,"created_at":"2012-06-27T09:12:25.000Z","updated_at":"2026-03-15T18:51:31.000Z","published_at":"2012-06-27T09:12:25.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 a vector of positive integers\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[a = [ 3 2 4 ];]]\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:t\u003ecreate the matrix where the\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ei\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e th column contains the vector\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e1:a(i)\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e possibly padded with zeros:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[b = [ 1 1 1\\n      2 2 2\\n      3 0 3\\n      0 0 4 ];]]\u003e\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":611,"title":"surrounded matrix","description":"With a given matrix A (size m x n) create a matrix B (size m+2 x n+2) so that the matrix A is surrounded by ones:\r\n\r\n  A = [1 2 3\r\n       3 2 1]\r\n  \r\n  B = [1 1 1 1 1\r\n       1 1 2 3 1\r\n       1 3 2 1 1\r\n       1 1 1 1 1]\r\n\r\nor\r\n\r\n  A = 4\r\n\r\n  B = [ 1 1 1\r\n        1 4 1\r\n        1 1 1]","description_html":"\u003cp\u003eWith a given matrix A (size m x n) create a matrix B (size m+2 x n+2) so that the matrix A is surrounded by ones:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eA = [1 2 3\r\n     3 2 1]\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003eB = [1 1 1 1 1\r\n     1 1 2 3 1\r\n     1 3 2 1 1\r\n     1 1 1 1 1]\r\n\u003c/pre\u003e\u003cp\u003eor\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eA = 4\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003eB = [ 1 1 1\r\n      1 4 1\r\n      1 1 1]\r\n\u003c/pre\u003e","function_template":"function B = surrounded(A)\r\n  B = A;\r\nend","test_suite":"%%\r\nx = 1;\r\ny_correct = ones(3);\r\nassert(isequal(surrounded(x),y_correct))\r\n\r\n%%\r\nx = [3 3 3;3 3 3];\r\ny_correct = [1 1 1 1 1;1 3 3 3 1; 1 3 3 3 1; 1 1 1 1 1];\r\nassert(isequal(surrounded(x),y_correct))\r\n\r\n%%\r\nx = ones(3,4);\r\ny_correct = ones(5,6);\r\nassert(isequal(surrounded(x),y_correct))\r\n\r\n%%\r\nx = [0];\r\ny_correct = ones(3); y_correct(2,2) = 0;\r\nassert(isequal(surrounded(x),y_correct))\r\n\r\n%%\r\nx = reshape(1:9,[3,3]);\r\ny_correct = [1 1 1 1 1; 1 1 2 3 1; ...\r\n    1 4 5 6 1; 1 7 8 9 1; 1 1 1 1 1]';\r\nassert(isequal(surrounded(x),y_correct))\r\n","published":true,"deleted":false,"likes_count":6,"comments_count":2,"created_by":3062,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":1453,"test_suite_updated_at":"2017-02-24T16:54:28.000Z","rescore_all_solutions":false,"group_id":19,"created_at":"2012-04-22T09:21:22.000Z","updated_at":"2026-04-02T19:15:31.000Z","published_at":"2012-04-22T09:24:58.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\u003eWith a given matrix A (size m x n) create a matrix B (size m+2 x n+2) so that the matrix A is surrounded by ones:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[A = [1 2 3\\n     3 2 1]\\n\\nB = [1 1 1 1 1\\n     1 1 2 3 1\\n     1 3 2 1 1\\n     1 1 1 1 1]]]\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:t\u003eor\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[A = 4\\n\\nB = [ 1 1 1\\n      1 4 1\\n      1 1 1]]]\u003e\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":497,"title":"Make an awesome ramp for a tiny motorcycle stuntman","description":"Okay, given a vector, say v=[1 3 6 9 11], turn it into a matrix 'ramp' like so:\r\n\r\n  m=[1 3 6 9 11;\r\n     3 6 9 11 0;\r\n     6 9 11 0 0;\r\n     9 11 0 0 0;\r\n     11 0 0 0 0]\r\n\r\nThat way a tiny motorcycle stuntman can follow the trace and jump over 9 school buses safely.","description_html":"\u003cp\u003eOkay, given a vector, say v=[1 3 6 9 11], turn it into a matrix 'ramp' like so:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003em=[1 3 6 9 11;\r\n   3 6 9 11 0;\r\n   6 9 11 0 0;\r\n   9 11 0 0 0;\r\n   11 0 0 0 0]\r\n\u003c/pre\u003e\u003cp\u003eThat way a tiny motorcycle stuntman can follow the trace and jump over 9 school buses safely.\u003c/p\u003e","function_template":"function ramp = vrooom(v)\r\n  ramp = zeros(numel(v));\r\nend","test_suite":"%%\r\nv = [1 3 6 9 11];\r\ny_correct = [1 3 6 9 11;\r\n   3 6 9 11 0;\r\n   6 9 11 0 0;\r\n   9 11 0 0 0;\r\n   11 0 0 0 0];\r\nassert(isequal(vrooom(v),y_correct))\r\n\r\n%%\r\nv = [1 2 3];\r\ny_correct = [1 2 3;\r\n   2 3 0;\r\n   3 0 0];\r\nassert(isequal(vrooom(v),y_correct))\r\n\r\n%%\r\nv = [-1 0 2 8];\r\ny_correct = [-1 0 2 8;\r\n0 2 8 0;\r\n2 8 0 0;\r\n8 0 0 0];\r\nassert(isequal(vrooom(v),y_correct))\r\n","published":true,"deleted":false,"likes_count":6,"comments_count":3,"created_by":1675,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":750,"test_suite_updated_at":"2012-03-15T04:23:51.000Z","rescore_all_solutions":false,"group_id":19,"created_at":"2012-03-15T04:13:09.000Z","updated_at":"2026-03-15T18:45:42.000Z","published_at":"2012-03-15T21:15:55.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\u003eOkay, given a vector, say v=[1 3 6 9 11], turn it into a matrix 'ramp' like so:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[m=[1 3 6 9 11;\\n   3 6 9 11 0;\\n   6 9 11 0 0;\\n   9 11 0 0 0;\\n   11 0 0 0 0]]]\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:t\u003eThat way a tiny motorcycle stuntman can follow the trace and jump over 9 school buses safely.\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":300,"title":"Remove NaN ?","description":"input -\u003e matrix (n*m) with  at least one element equal to NaN;\r\noutput -\u003e matrix(p*m), the same matrix where we deleted the entire line containing NaN;","description_html":"\u003cp\u003einput -\u003e matrix (n*m) with  at least one element equal to NaN;\r\noutput -\u003e matrix(p*m), the same matrix where we deleted the entire line containing NaN;\u003c/p\u003e","function_template":"function y = removeNaN(x)\r\n  y = x;\r\nend","test_suite":"%%\r\nx = magic(3);x(2,[1 3])= NaN;\r\ny_correct = [8 1 6;4 9 2];\r\nassert(isequal(removeNaN(x),y_correct))\r\n\r\n%%\r\nx = [magic(4) ones(4,1)];\r\nx(4,5) = NaN;x(3,2) = Inf;x(3,1)=NaN;\r\ny_correct = x(1:2,:);\r\nassert(isequal(removeNaN(x),y_correct))\r\n\r\n%%\r\nx = ones(4);x(3,3)=NaN;\r\ny_correct = ones(3,4);\r\nassert(isequal(removeNaN(x),y_correct))\r\n\r\n%%\r\nx = [1; NaN];\r\ny_correct = [1];\r\nassert(isequal(removeNaN(x),y_correct))\r\n\r\n%%\r\nx = NaN;\r\nassert(isempty(removeNaN(x)))\r\n\r\n%%\r\nx = [1:10 NaN];\r\nassert(isempty(removeNaN(x)))\r\n\r\n%%\r\nx = [1:10 NaN]';\r\ny_correct = [1:10]';\r\nassert(isequal(removeNaN(x),y_correct))\r\n","published":true,"deleted":false,"likes_count":13,"comments_count":3,"created_by":1309,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":1354,"test_suite_updated_at":"2017-02-24T16:46:34.000Z","rescore_all_solutions":false,"group_id":19,"created_at":"2012-02-09T22:17:06.000Z","updated_at":"2026-04-02T15:06:03.000Z","published_at":"2012-02-09T23:49:31.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\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"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\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\u003einput -\u003e matrix (n*m) with at least one element equal to NaN; output -\u003e matrix(p*m), the same matrix where we deleted the entire line containing NaN;\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":798,"title":"Rotate input square matrix 90 degrees CCW without rot90","description":"Rotate input matrix (which will be square) 90 degrees counter-clockwise without using rot90,flipud,fliplr, or flipdim (or eval).  \r\n\r\nExample input: [ 1 2 3; 4 5 6; 7 8 9]\r\n\r\nExample output: [ 3 6 9; 2 5 8; 1 4 7]\r\n\r\nBecause of simplistic regexp tests (sorry), you shouldn't use variable names or strings composed of the outlawed function names.","description_html":"\u003cp\u003eRotate input matrix (which will be square) 90 degrees counter-clockwise without using rot90,flipud,fliplr, or flipdim (or eval).\u003c/p\u003e\u003cp\u003eExample input: [ 1 2 3; 4 5 6; 7 8 9]\u003c/p\u003e\u003cp\u003eExample output: [ 3 6 9; 2 5 8; 1 4 7]\u003c/p\u003e\u003cp\u003eBecause of simplistic regexp tests (sorry), you shouldn't use variable names or strings composed of the outlawed function names.\u003c/p\u003e","function_template":"function y = rot_hardway(x)\r\n  y = x;\r\nend","test_suite":"%%\r\nassert(isempty(regexp(evalc('type rot_hardway'),'(eval|fliplr|flipud|flipdim|rot90|)')))\r\n\r\n%%\r\nx = [1 2 3;4 5 6;7 8 9];\r\ny_correct = [3 6 9;2 5 8;1 4 7];\r\nassert(isequal(rot_hardway(x),y_correct))\r\n\r\n%%\r\nx = [sqrt(-1) 2 3;4 5 6;7 8 9];\r\ny_correct = [3 6 9;2 5 8;sqrt(-1) 4 7];\r\nassert(isequal(rot_hardway(x),y_correct))\r\n\r\n%%\r\nx = magic(9);\r\ny_correct = rot90(magic(9));\r\nassert(isequal(rot_hardway(x),y_correct))\r\n","published":true,"deleted":false,"likes_count":5,"comments_count":3,"created_by":2846,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":683,"test_suite_updated_at":"2017-02-24T16:59:54.000Z","rescore_all_solutions":false,"group_id":19,"created_at":"2012-06-27T19:21:15.000Z","updated_at":"2026-03-15T18:53:31.000Z","published_at":"2012-06-27T19:21:15.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\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"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\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\u003eRotate input matrix (which will be square) 90 degrees counter-clockwise without using rot90,flipud,fliplr, or flipdim (or eval).\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExample input: [ 1 2 3; 4 5 6; 7 8 9]\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExample output: [ 3 6 9; 2 5 8; 1 4 7]\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eBecause of simplistic regexp tests (sorry), you shouldn't use variable names or strings composed of the outlawed function names.\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":858,"title":"Permute diagonal and antidiagonal","description":"Permute diagonal and antidiagonal\r\nFor example [1 2 3;4 5 6;7 8 9] -\u003e [3 2 1;4 5 6;9 8 7]\r\nWITHOUT diag function (and variable name)\r\n","description_html":"\u003cp\u003ePermute diagonal and antidiagonal\r\nFor example [1 2 3;4 5 6;7 8 9] -\u003e [3 2 1;4 5 6;9 8 7]\r\nWITHOUT diag function (and variable name)\u003c/p\u003e","function_template":"function y = permuted(x)\r\n  y = withoutdiag(x);\r\nend","test_suite":"%%\r\nx = [1 2 3;4 5 6;7 8 9];\r\ny_correct = [3 2 1;4 5 6;9 8 7];\r\nfiletext = fileread('permuted.m');\r\nassert(isequal(permuted(x),y_correct))\r\nassert(isempty(strfind(filetext, 'diag')))\r\n%%\r\nx=[1 2;3 4];\r\ny_correct = [2 1;4 3];\r\nfiletext = fileread('permuted.m');\r\nassert(isequal(permuted(x),y_correct))\r\nassert(isempty(strfind(filetext, 'diag')))\r\n%%\r\nx=[1 2;3 4];\r\ny_correct = [2 1;4 3];\r\nfiletext = fileread('permuted.m');\r\nassert(isequal(permuted(x),y_correct))\r\nassert(isempty(strfind(filetext, 'diag')))\r\n%%\r\nx=magic(10);\r\ny_correct = [    40    99     1     8    15    67    74    51    58    92\r\n    98    64     7    14    16    73    55    57    80    41\r\n     4    81    63    20    22    54    56    88    70    47\r\n    85    87    19    62     3    60    21    69    71    28\r\n    86    93    25     2    61     9    68    75    52    34\r\n    17    24    76    83    42    90    49    26    33    65\r\n    23     5    82    30    91    48    89    32    39    66\r\n    79     6    38    95    97    29    31    13    45    72\r\n    10    46    94    96    78    35    37    44    12    53\r\n    59    18   100    77    84    36    43    50    27    11];\r\nfiletext = fileread('permuted.m');\r\nassert(isequal(permuted(x),y_correct))\r\nassert(isempty(strfind(filetext, 'diag')))\r\n\r\n\r\n\r\n","published":true,"deleted":false,"likes_count":7,"comments_count":1,"created_by":5390,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":505,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":19,"created_at":"2012-07-24T01:19:14.000Z","updated_at":"2026-04-02T19:11:56.000Z","published_at":"2012-07-24T01:19:14.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\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"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\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\u003ePermute diagonal and antidiagonal For example [1 2 3;4 5 6;7 8 9] -\u003e [3 2 1;4 5 6;9 8 7] WITHOUT diag function (and variable name)\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":1042,"title":"Operate on matrices of unequal, yet similar, size","description":"You may want to add a vector to a matrix, implying that the vector is added to each column of the matrix. Or multiply a 3x4x5 matrix with a 3x4 matrix, intending to do that for every element in the 3th dimension. \r\n\r\n  \u003e\u003e [1 2;3 4].*[1 2]\r\n  \r\n  ans =\r\n  \r\n     1     4\r\n     3     8\r\n\r\nIt's clear what you want to do, but Matlab is very strict and gives an error, unless you first make the size of the arguments match.\r\n\r\nYou will now create a function that operates on two arguments, performing one of the operations 'plus', 'minus', 'times', 'power', 'ldivide' or 'rdivide' (specified as the 3rd argument), after it has matched the sizes of first two input arguments.\r\n\r\nIt should work on input arguments of more than two dimensions, of arguments of different dimensions, as long as they are numerical matrices, vectors, or values.\r\n\r\nThere is one limitation: for dimensions in which the size of the input arguments differ, the size of one of them should be one.\r\n\r\nNote: Solutions wrapped in eval(c), inline, str2func, regexprep (dynamic regular expressions), etc, are not appreciated.","description_html":"\u003cp\u003eYou may want to add a vector to a matrix, implying that the vector is added to each column of the matrix. Or multiply a 3x4x5 matrix with a 3x4 matrix, intending to do that for every element in the 3th dimension.\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e\u003e\u003e [1 2;3 4].*[1 2]\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003eans =\r\n\u003c/pre\u003e\u003cpre\u003e     1     4\r\n     3     8\u003c/pre\u003e\u003cp\u003eIt's clear what you want to do, but Matlab is very strict and gives an error, unless you first make the size of the arguments match.\u003c/p\u003e\u003cp\u003eYou will now create a function that operates on two arguments, performing one of the operations 'plus', 'minus', 'times', 'power', 'ldivide' or 'rdivide' (specified as the 3rd argument), after it has matched the sizes of first two input arguments.\u003c/p\u003e\u003cp\u003eIt should work on input arguments of more than two dimensions, of arguments of different dimensions, as long as they are numerical matrices, vectors, or values.\u003c/p\u003e\u003cp\u003eThere is one limitation: for dimensions in which the size of the input arguments differ, the size of one of them should be one.\u003c/p\u003e\u003cp\u003eNote: Solutions wrapped in eval(c), inline, str2func, regexprep (dynamic regular expressions), etc, are not appreciated.\u003c/p\u003e","function_template":"function z = matched_op(x,y,op)\r\n  z = feval(op,x,y);\r\nend","test_suite":"%%\r\nnocheat = isempty(regexp(evalc('type matched_op'),'([^f]eval|regexprep|inline|str2func)'));\r\nx = [1 2;3 4];\r\ny = [1 2];\r\nz_correct = [1 4;3 8];\r\nassert(isequal(matched_op(x,y,'times'),z_correct) \u0026\u0026 nocheat)\r\n\r\n%%\r\nnocheat = isempty(regexp(evalc('type matched_op'),'([^f]eval|regexprep|inline|str2func)'));\r\nx = [10i];\r\ny = [20];\r\nz_correct = [-2i];\r\nassert(isequal(matched_op(x,y,'ldivide'),z_correct) \u0026\u0026 nocheat)\r\n\r\n%%\r\nnocheat = isempty(regexp(evalc('type matched_op'),'([^f]eval|regexprep|inline|str2func)'));\r\nx = reshape(1:4,[1 1 1 4]);\r\ny = (1:4).';\r\nz_correct = reshape([2 3 4 5 3 4 5 6 4 5 6 7 5 6 7 8],[4 1 1 4]);\r\nassert(isequal(matched_op(x,y,'plus'),z_correct) \u0026\u0026 nocheat)\r\n\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":3,"created_by":6556,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":229,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":19,"created_at":"2012-11-20T09:59:22.000Z","updated_at":"2026-03-15T18:54:46.000Z","published_at":"2012-11-20T09:59:31.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\u003eYou may want to add a vector to a matrix, implying that the vector is added to each column of the matrix. Or multiply a 3x4x5 matrix with a 3x4 matrix, intending to do that for every element in the 3th dimension.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[\u003e\u003e [1 2;3 4].*[1 2]\\n\\nans =\\n\\n     1     4\\n     3     8]]\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:t\u003eIt's clear what you want to do, but Matlab is very strict and gives an error, unless you first make the size of the arguments match.\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\u003eYou will now create a function that operates on two arguments, performing one of the operations 'plus', 'minus', 'times', 'power', 'ldivide' or 'rdivide' (specified as the 3rd argument), after it has matched the sizes of first two input arguments.\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\u003eIt should work on input arguments of more than two dimensions, of arguments of different dimensions, as long as they are numerical matrices, vectors, or 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:t\u003eThere is one limitation: for dimensions in which the size of the input arguments differ, the size of one of them should be one.\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: Solutions wrapped in eval(c), inline, str2func, regexprep (dynamic regular expressions), etc, are not appreciated.\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":792,"title":"Set some matrix elements to zero","description":"First get the maximum of each *row*, and afterwards set all the other elements to zero. For example, this matrix:\r\n\r\n  1 \t2 \t3 \t4\r\n  5 \t5 \t6 \t5\r\n  7 \t9 \t8 \t3\r\n  \r\nshould become:\r\n\r\n  0 \t0 \t0 \t4\r\n  0 \t0 \t6 \t0\r\n  0 \t9 \t0 \t0\r\n\r\nInput will never be empty.\r\n\r\nIf a row has the same maximum in different columns , display the first occurrence:\r\n\r\nIn the below example, the second row has twice the number 8 as maximum , the output will display the first 8 (column2) :\r\n\r\n   x =\r\n  \r\n       5     4     5\r\n       2     8     8\r\n\r\n\r\n   should become :\r\n\r\n     \r\n     5     0     0\r\n     0     8     0\r\n","description_html":"\u003cp\u003eFirst get the maximum of each \u003cb\u003erow\u003c/b\u003e, and afterwards set all the other elements to zero. For example, this matrix:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e1 \t2 \t3 \t4\r\n5 \t5 \t6 \t5\r\n7 \t9 \t8 \t3\r\n\u003c/pre\u003e\u003cp\u003eshould become:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e0 \t0 \t0 \t4\r\n0 \t0 \t6 \t0\r\n0 \t9 \t0 \t0\r\n\u003c/pre\u003e\u003cp\u003eInput will never be empty.\u003c/p\u003e\u003cp\u003eIf a row has the same maximum in different columns , display the first occurrence:\u003c/p\u003e\u003cp\u003eIn the below example, the second row has twice the number 8 as maximum , the output will display the first 8 (column2) :\u003c/p\u003e\u003cpre\u003e   x =\u003c/pre\u003e\u003cpre\u003e       5     4     5\r\n       2     8     8\u003c/pre\u003e\u003cpre\u003e   should become :\u003c/pre\u003e\u003cpre\u003e     5     0     0\r\n     0     8     0\u003c/pre\u003e","function_template":"function y = your_fcn_name(x)\r\n  y = x;\r\nend","test_suite":"%%\r\nx = [1     2     3     4\r\n     5     5     6     5\r\n     7     9     8     3];\r\ny_correct = [0 \t0 0 4\r\n             0 \t0 6 0\r\n             0 \t9 0 0];\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n\r\n%% \r\nx = magic(4);\r\ny_correct  = [16     0     0     0\r\n     0    11     0     0\r\n     0     0     0    12\r\n     0     0    15     0];\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n\r\n%% \r\nx = pi;\r\ny_correct  = pi;\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n\r\n%% \r\nx = 0;\r\ny_correct  = 0;\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n\r\n%%\r\n x = toeplitz(1:5);\r\ny_correct  = [   0     0     0     0     5\r\n     0     0     0     0     4\r\n     3     0     0     0     0\r\n     4     0     0     0     0\r\n     5     0     0     0     0];\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n\r\n%% \r\nx = ones(5);\r\ny_correct  = [ 1     0     0     0     0\r\n     1     0     0     0     0\r\n     1     0     0     0     0\r\n     1     0     0     0     0\r\n     1     0     0     0     0];\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n\r\n%%\r\nx =nchoosek(7:9,3);\r\ny_correct  =[ 0     0     9];\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n\r\n%%\r\nx =nchoosek(4:9,2);\r\ny_correct  =  [ 0     5\r\n     0     6\r\n     0     7\r\n     0     8\r\n     0     9\r\n     0     6\r\n     0     7\r\n     0     8\r\n     0     9\r\n     0     7\r\n     0     8\r\n     0     9\r\n     0     8\r\n     0     9\r\n     0     9];\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n","published":true,"deleted":false,"likes_count":8,"comments_count":1,"created_by":639,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":626,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":19,"created_at":"2012-06-27T07:35:57.000Z","updated_at":"2026-03-28T02:33:55.000Z","published_at":"2012-06-27T07:35: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\u003eFirst get the maximum of each\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003erow\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, and afterwards set all the other elements to zero. For example, this matrix:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[1   2   3   4\\n5   5   6   5\\n7   9   8   3]]\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:t\u003eshould become:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[0   0   0   4\\n0   0   6   0\\n0   9   0   0]]\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:t\u003eInput will never be empty.\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\u003eIf a row has the same maximum in different columns , display the first occurrence:\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\u003eIn the below example, the second row has twice the number 8 as maximum , the output will display the first 8 (column2) :\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[   x =\\n\\n       5     4     5\\n       2     8     8\\n\\n   should become :\\n\\n     5     0     0\\n     0     8     0]]\u003e\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":796,"title":"Removing rows from a matrix is easy - but what about inserting rows?","description":"Assume A is a 5-by-5 matrix. A([2,4],:) = [] is a quick way to remove rows 2 and 4. Can you find a quick way to insert rows into A?\r\n\r\nYou are given a n-by-m matrix A, a 1-by-p vector of indices IND, and either a scalar value (1-by-1) or a p-by-m matrix b to be inserted AFTER rows IND of A. In case of a scalar value it should fill the whole row. Note that IND may contain duplicate indices, which means that more than one row should be inserted after that specific row index.\r\n\r\nFor example:\r\n\r\n   A = [1 1; \r\n        3 3; \r\n        4 4];\r\n   IND = [1 3];\r\n   b = [2 2; \r\n        5 5];\r\n\r\nshould become\r\n\r\n   y = [1 1; \r\n        2 2; \r\n        3 3; \r\n        4 4; \r\n        5 5];\r\n\r\nAnd\r\n\r\n   A = [0 0; \r\n        1 1];\r\n   IND = [1 1 2];\r\n   b = NaN;\r\n\r\nshould become\r\n\r\n   y = [0   0; \r\n        NaN NaN; \r\n        NaN NaN; \r\n        1 1; \r\n        NaN NaN];\r\n","description_html":"\u003cp\u003eAssume A is a 5-by-5 matrix. A([2,4],:) = [] is a quick way to remove rows 2 and 4. Can you find a quick way to insert rows into A?\u003c/p\u003e\u003cp\u003eYou are given a n-by-m matrix A, a 1-by-p vector of indices IND, and either a scalar value (1-by-1) or a p-by-m matrix b to be inserted AFTER rows IND of A. In case of a scalar value it should fill the whole row. Note that IND may contain duplicate indices, which means that more than one row should be inserted after that specific row index.\u003c/p\u003e\u003cp\u003eFor example:\u003c/p\u003e\u003cpre\u003e   A = [1 1; \r\n        3 3; \r\n        4 4];\r\n   IND = [1 3];\r\n   b = [2 2; \r\n        5 5];\u003c/pre\u003e\u003cp\u003eshould become\u003c/p\u003e\u003cpre\u003e   y = [1 1; \r\n        2 2; \r\n        3 3; \r\n        4 4; \r\n        5 5];\u003c/pre\u003e\u003cp\u003eAnd\u003c/p\u003e\u003cpre\u003e   A = [0 0; \r\n        1 1];\r\n   IND = [1 1 2];\r\n   b = NaN;\u003c/pre\u003e\u003cp\u003eshould become\u003c/p\u003e\u003cpre\u003e   y = [0   0; \r\n        NaN NaN; \r\n        NaN NaN; \r\n        1 1; \r\n        NaN NaN];\u003c/pre\u003e","function_template":"function y = insertRows(A,IND,b)\r\n  y = x;\r\nend","test_suite":"%%\r\nA = 1;\r\nIND = 1;\r\nb = 1;\r\ny_correct = [1;1];\r\nassert(isequal(insertRows(A, IND, b), y_correct))\r\n\r\n%%\r\nA = [0 0; 1 1];\r\nIND = [1 1 1 2];\r\nb = NaN;\r\ny_correct = [0 0;NaN NaN;NaN NaN;NaN NaN;1 1;NaN NaN];\r\nassert(isequalwithequalnans(insertRows(A, IND, b), y_correct))\r\n\r\n%%\r\nA = [1 1; 3 3; 4 4];\r\nIND = [1 3];\r\nb = [2 2; 5 5];\r\ny_correct = [1 1;2 2;3 3;4 4;5 5];\r\nassert(isequal(insertRows(A, IND, b), y_correct))\r\n\r\n%%\r\nA = (1:2:10)';\r\nIND = 1:5;\r\nb = (2:2:10)';\r\ny_correct = (1:10)';\r\nassert(isequal(insertRows(A, IND, b), y_correct))\r\n\r\n%%\r\nA = zeros(10,5);\r\nIND = [3,7];\r\nb = rand(2,5);\r\ny_correct = [zeros(3,5); b(1,:); zeros(4,5); b(2,:); zeros(3,5)];\r\nassert(isequal(insertRows(A, IND, b), y_correct))\r\n\r\n%%\r\nassert(isempty(strfind(evalc('type insertRows'), 'regexp')));","published":true,"deleted":false,"likes_count":3,"comments_count":4,"created_by":4976,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":265,"test_suite_updated_at":"2013-10-20T11:51:38.000Z","rescore_all_solutions":false,"group_id":19,"created_at":"2012-06-27T17:17:54.000Z","updated_at":"2026-03-15T18:52:34.000Z","published_at":"2012-06-27T17:25:07.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\u003eAssume A is a 5-by-5 matrix. A([2,4],:) = [] is a quick way to remove rows 2 and 4. Can you find a quick way to insert rows into A?\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\u003eYou are given a n-by-m matrix A, a 1-by-p vector of indices IND, and either a scalar value (1-by-1) or a p-by-m matrix b to be inserted AFTER rows IND of A. In case of a scalar value it should fill the whole row. Note that IND may contain duplicate indices, which means that more than one row should be inserted after that specific row index.\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\u003eFor example:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[   A = [1 1; \\n        3 3; \\n        4 4];\\n   IND = [1 3];\\n   b = [2 2; \\n        5 5];]]\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:t\u003eshould become\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[   y = [1 1; \\n        2 2; \\n        3 3; \\n        4 4; \\n        5 5];]]\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:t\u003eAnd\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[   A = [0 0; \\n        1 1];\\n   IND = [1 1 2];\\n   b = NaN;]]\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:t\u003eshould become\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[   y = [0   0; \\n        NaN NaN; \\n        NaN NaN; \\n        1 1; \\n        NaN NaN];]]\u003e\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":112,"title":"Remove the air bubbles","description":"Given a matrix a, return a matrix b in which all the zeros have \"bubbled\" to the top. That is, any zeros in a given column should be moved to the top. The order of the remaining nonzero numbers in the column should be preserved.\r\n\r\nExample 1\r\n\r\n Input  a  = [ 1 2 3\r\n               0 4 5\r\n               6 0 0 ]\r\n  \r\n Output b is [ 0 0 0\r\n               1 2 3\r\n               6 4 5 ]\r\n\r\nExample 2\r\n\r\n Input  a  = [ 1 0 5 0 6 0 7 ]'\r\n Output b is [ 0 0 0 1 5 6 7 ]'\r\n\r\nExample 3\r\n\r\n Input  a  = [ 1 0\r\n               1 1 ]\r\n Output b is [ 1 0 \r\n               1 1 ]\r\n","description_html":"\u003cp\u003eGiven a matrix a, return a matrix b in which all the zeros have \"bubbled\" to the top. That is, any zeros in a given column should be moved to the top. The order of the remaining nonzero numbers in the column should be preserved.\u003c/p\u003e\u003cp\u003eExample 1\u003c/p\u003e\u003cpre\u003e Input  a  = [ 1 2 3\r\n               0 4 5\r\n               6 0 0 ]\u003c/pre\u003e\u003cpre\u003e Output b is [ 0 0 0\r\n               1 2 3\r\n               6 4 5 ]\u003c/pre\u003e\u003cp\u003eExample 2\u003c/p\u003e\u003cpre\u003e Input  a  = [ 1 0 5 0 6 0 7 ]'\r\n Output b is [ 0 0 0 1 5 6 7 ]'\u003c/pre\u003e\u003cp\u003eExample 3\u003c/p\u003e\u003cpre\u003e Input  a  = [ 1 0\r\n               1 1 ]\r\n Output b is [ 1 0 \r\n               1 1 ]\u003c/pre\u003e","function_template":"function b = bubbles(a)\r\n  b = [];\r\nend","test_suite":"%%\r\n\r\na = [ 1 2 3\r\n      0 4 5\r\n      6 0 0 ];\r\nb_correct = [ 0 0 0\r\n       1 2 3\r\n       6 4 5 ];\r\nassert(isequal(bubbles(a),b_correct))\r\n\r\n%%\r\n\r\na = [ 1 0 5 0 7 0 6 ]';\r\nb_correct = [ 0 0 0 1 5 7 6 ]';\r\nassert(isequal(bubbles(a),b_correct))\r\n\r\n%%\r\n\r\na = [1 0; 1 1];\r\nb_correct = [1 0; 1 1];\r\nassert(isequal(bubbles(a),b_correct))\r\n\r\n%%\r\n\r\na = [0 8 0 6 -2]';\r\nb_correct = [0 0 8 6 -2]';\r\nassert(isequal(bubbles(a),b_correct))\r\n","published":true,"deleted":false,"likes_count":21,"comments_count":5,"created_by":7,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":1332,"test_suite_updated_at":"2012-01-29T23:46:46.000Z","rescore_all_solutions":false,"group_id":19,"created_at":"2012-01-26T20:27:23.000Z","updated_at":"2026-04-03T20:05:10.000Z","published_at":"2012-01-29T23:54:21.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 a matrix a, return a matrix b in which all the zeros have \\\"bubbled\\\" to the top. That is, any zeros in a given column should be moved to the top. The order of the remaining nonzero numbers in the column should be preserved.\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\u003eExample 1\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ Input  a  = [ 1 2 3\\n               0 4 5\\n               6 0 0 ]\\n\\n Output b is [ 0 0 0\\n               1 2 3\\n               6 4 5 ]]]\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:t\u003eExample 2\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ Input  a  = [ 1 0 5 0 6 0 7 ]'\\n Output b is [ 0 0 0 1 5 6 7 ]']]\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:t\u003eExample 3\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ Input  a  = [ 1 0\\n               1 1 ]\\n Output b is [ 1 0 \\n               1 1 ]]]\u003e\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":375,"title":"N-Dimensional Array Slice","description":"Given an N-dimensional array, _A_, an index, _I_, and a dimension, _d_, return the _I_ th elements of _A_ in the _d_ dimension.\r\n\r\nFor Example,\r\n\r\n    array_slice( A, 5, 3 )\r\n\r\nis equivalent to\r\n\r\n    A(:,:,5)\r\n\r\nNote: |eval| and |str2func| cannot be used. This is a Cody restriction.","description_html":"\u003cp\u003eGiven an N-dimensional array, \u003ci\u003eA\u003c/i\u003e, an index, \u003ci\u003eI\u003c/i\u003e, and a dimension, \u003ci\u003ed\u003c/i\u003e, return the \u003ci\u003eI\u003c/i\u003e th elements of \u003ci\u003eA\u003c/i\u003e in the \u003ci\u003ed\u003c/i\u003e dimension.\u003c/p\u003e\u003cp\u003eFor Example,\u003c/p\u003e\u003cpre\u003e    array_slice( A, 5, 3 )\u003c/pre\u003e\u003cp\u003eis equivalent to\u003c/p\u003e\u003cpre\u003e    A(:,:,5)\u003c/pre\u003e\u003cp\u003eNote: \u003ctt\u003eeval\u003c/tt\u003e and \u003ctt\u003estr2func\u003c/tt\u003e cannot be used. This is a Cody restriction.\u003c/p\u003e","function_template":"function S = arraySlice(A,I,d)\r\n  S = A(:,I);\r\nend","test_suite":"%%\r\nA = randn(5,5);\r\nassert(isequal(arraySlice(A,4,2),A(:,4)))\r\n\r\n%%\r\nA = randn(5,5);\r\nassert(isequal(arraySlice(A,4,1),A(4,:)))\r\n\r\n%%\r\nA = randn(5,5);\r\nassert(isequal(arraySlice(A,1,10),A))\r\n\r\n%%\r\nA = randn(5,5,5,3);\r\nassert(isequal(arraySlice(A,3,4),A(:,:,:,3)))\r\n\r\n%%\r\nA = randn(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2);\r\nassert(isequal(arraySlice(A,2,18),A(:,:,:,:,:,:,:,:,:,:,:,:,:,:,:,:,:,2)))","published":true,"deleted":false,"likes_count":13,"comments_count":7,"created_by":134,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":284,"test_suite_updated_at":"2012-02-21T16:23:06.000Z","rescore_all_solutions":false,"group_id":19,"created_at":"2012-02-21T16:23:06.000Z","updated_at":"2026-04-01T12:13:28.000Z","published_at":"2012-02-21T16:23: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\u003eGiven an N-dimensional array,\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eA\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, an index,\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eI\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, and a dimension,\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ed\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, return the\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eI\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e th elements of\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eA\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e in the\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ed\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e dimension.\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\u003eFor Example,\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[    array_slice( A, 5, 3 )]]\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:t\u003eis equivalent to\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[    A(:,:,5)]]\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:t\u003eNote:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eeval\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e and\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003estr2func\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e cannot be used. This is a Cody restriction.\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":782,"title":"Some Assembly Required","description":"The input to this function is a matrix of real numbers.  Your job is to assemble the rows of the matrix into one large row that contains all of the individual rows of the matrix, and to make this output row as short as possible.  You accomplish this by joining the rows together at the points at which they overlap.  To help with the task, you can flip rows if you need to do so.\r\n\r\nFor example:\r\n\r\n  input  = [1 1 2 3 ; 4 5 6 7 ; 5 4 8 3]\r\n\r\n  output = [1 1 2 3 8 4 5 6 7]\r\n\r\nExplanation:\r\nThe [1 1 2 3] is the first row of the input.\r\n\r\n[3 8 4 5] is the third row (flipped) so the 3 at the end of the third row overlaps with the 3 at the end of the first row.\r\n\r\n[4 5 6 7] is the second row, with the [4 5] overlapping with the flipped [5 4] from the third row.\r\n\r\nOther than the mirrored version of the solution ([7 6 5 4 8 3 2 1 1] in the above example), each solution will be unique.  Flipped versions of the entered value of y_correct will be tested for as well, in case your code comes up with that version of the correct answer.\r\n\r\nGood luck, and happy hunting.","description_html":"\u003cp\u003eThe input to this function is a matrix of real numbers.  Your job is to assemble the rows of the matrix into one large row that contains all of the individual rows of the matrix, and to make this output row as short as possible.  You accomplish this by joining the rows together at the points at which they overlap.  To help with the task, you can flip rows if you need to do so.\u003c/p\u003e\u003cp\u003eFor example:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003einput  = [1 1 2 3 ; 4 5 6 7 ; 5 4 8 3]\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003eoutput = [1 1 2 3 8 4 5 6 7]\r\n\u003c/pre\u003e\u003cp\u003eExplanation:\r\nThe [1 1 2 3] is the first row of the input.\u003c/p\u003e\u003cp\u003e[3 8 4 5] is the third row (flipped) so the 3 at the end of the third row overlaps with the 3 at the end of the first row.\u003c/p\u003e\u003cp\u003e[4 5 6 7] is the second row, with the [4 5] overlapping with the flipped [5 4] from the third row.\u003c/p\u003e\u003cp\u003eOther than the mirrored version of the solution ([7 6 5 4 8 3 2 1 1] in the above example), each solution will be unique.  Flipped versions of the entered value of y_correct will be tested for as well, in case your code comes up with that version of the correct answer.\u003c/p\u003e\u003cp\u003eGood luck, and happy hunting.\u003c/p\u003e","function_template":"function y = assemble_this(x)\r\n  y = x;\r\nend","test_suite":"%%\r\nx= [1 2 ; 2 3 ; 3 4 ; 4 5];\r\ny_correct=[1 2 3 4 5];\r\nassert(isequal(assemble_this(x),y_correct)||isequal(assemble_this(x),fliplr(y_correct)))\r\n%%\r\nx= [1 1 2 3 ; 4 5 6 7 ; 5 4 8 3];\r\ny_correct=[1 1 2 3 8 4 5 6 7];\r\nassert(isequal(assemble_this(x),y_correct)||isequal(assemble_this(x),fliplr(y_correct)))\r\n%%\r\nx=[2 3 ; 4 2 ; 3 1 ; 1 5 ; 5 9];\r\ny_correct=[9 5 1 3 2 4];\r\nassert(isequal(assemble_this(x),y_correct)||isequal(assemble_this(x),fliplr(y_correct)))\r\n%%\r\nx=[10:-1:6 ; 1:5 ; 5:0.25:6];\r\ny_correct=[1:4 5:0.25:6 7:10];\r\nassert(isequal(assemble_this(x),y_correct)||isequal(assemble_this(x),fliplr(y_correct)))\r\n%%\r\nx=[8 16 24 ; 2 4 8 ; 6 4 2];\r\ny_correct=[6 4 2 4 8 16 24];\r\nassert(isequal(assemble_this(x),y_correct)||isequal(assemble_this(x),fliplr(y_correct)))\r\n%%\r\ny=ceil(rand*7)+5;\r\nry=[rand(1,y) y];\r\nfry=fliplr([y ry(1:end-1)]);\r\natf=assemble_this([fry ; ry]);\r\ny_correct=[y fry];\r\nassert(isequal(atf,y_correct)||isequal(atf,fliplr(y_correct)))\r\n%%\r\nt=rand(1,2);\r\nx=[8 16 24 ; 2 4 8 ; 6 4 2 ; 24 t];\r\nat=assemble_this(x);\r\ny_correct=[fliplr(t) 24 16 8 4 2 4 6];\r\nassert(isequal(at,y_correct)||isequal(at,fliplr(y_correct)))\r\n%%\r\nk=5+ceil(8*rand);\r\nx=randperm(k);\r\ny=randperm(k)+k;\r\nat=assemble_this([x x ; x y]);\r\ny_correct=[x x y];\r\nassert(isequal(at,y_correct)||isequal(at,fliplr(y_correct)))\r\n","published":true,"deleted":false,"likes_count":9,"comments_count":6,"created_by":1615,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":130,"test_suite_updated_at":"2018-01-08T18:23:27.000Z","rescore_all_solutions":true,"group_id":19,"created_at":"2012-06-21T18:36:35.000Z","updated_at":"2026-04-03T20:25:05.000Z","published_at":"2012-06-21T18:43:48.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 input to this function is a matrix of real numbers. Your job is to assemble the rows of the matrix into one large row that contains all of the individual rows of the matrix, and to make this output row as short as possible. You accomplish this by joining the rows together at the points at which they overlap. To help with the task, you can flip rows if you need to do so.\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\u003eFor example:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[input  = [1 1 2 3 ; 4 5 6 7 ; 5 4 8 3]\\n\\noutput = [1 1 2 3 8 4 5 6 7]]]\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:t\u003eExplanation: The [1 1 2 3] is the first row of the input.\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[3 8 4 5] is the third row (flipped) so the 3 at the end of the third row overlaps with the 3 at the end of the first row.\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[4 5 6 7] is the second row, with the [4 5] overlapping with the flipped [5 4] from the third row.\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\u003eOther than the mirrored version of the solution ([7 6 5 4 8 3 2 1 1] in the above example), each solution will be unique. Flipped versions of the entered value of y_correct will be tested for as well, in case your code comes up with that version of the correct answer.\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\u003eGood luck, and happy hunting.\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":411,"title":"Back to basics 21 - Matrix replicating","description":"Covering some basic topics I haven't seen elsewhere on Cody.\r\n\r\nGiven an input matrix, generate an output matrix that consists of the input matrix replicated in a 2 by 2 pattern Examples:\r\n\r\n  [1] -\u003e [1 1\r\n          1 1]\r\n\r\n  [1 2       -\u003e [1 2 1 2\r\n   3 4]          3 4 3 4 \r\n                 1 2 1 2 \r\n                 3 4 3 4]","description_html":"\u003cp\u003eCovering some basic topics I haven't seen elsewhere on Cody.\u003c/p\u003e\u003cp\u003eGiven an input matrix, generate an output matrix that consists of the input matrix replicated in a 2 by 2 pattern Examples:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e[1] -\u003e [1 1\r\n        1 1]\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003e[1 2       -\u003e [1 2 1 2\r\n 3 4]          3 4 3 4 \r\n               1 2 1 2 \r\n               3 4 3 4]\r\n\u003c/pre\u003e","function_template":"function y = matrix_replication(x)\r\n  y = x;\r\nend","test_suite":"%%\r\nx = [1];\r\ny_correct = [1 1;1 1];\r\nassert(isequal(matrix_replication(x),y_correct))\r\n\r\n%%\r\nx = [1 2;3 4];\r\ny_correct = [1 2 1 2; 3 4 3 4; 1 2 1 2; 3 4 3 4];\r\nassert(isequal(matrix_replication(x),y_correct))\r\n\r\n\r\n%%\r\nx = [1 2];\r\ny_correct = [1 2 1 2; 1 2 1 2];\r\nassert(isequal(matrix_replication(x),y_correct))","published":true,"deleted":false,"likes_count":8,"comments_count":0,"created_by":1022,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":1798,"test_suite_updated_at":"2012-02-25T21:33:19.000Z","rescore_all_solutions":false,"group_id":19,"created_at":"2012-02-25T21:33:19.000Z","updated_at":"2026-03-28T23:57:05.000Z","published_at":"2012-02-25T21:33:19.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\u003eCovering some basic topics I haven't seen elsewhere on Cody.\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\u003eGiven an input matrix, generate an output matrix that consists of the input matrix replicated in a 2 by 2 pattern Examples:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[[1] -\u003e [1 1\\n        1 1]\\n\\n[1 2       -\u003e [1 2 1 2\\n 3 4]          3 4 3 4 \\n               1 2 1 2 \\n               3 4 3 4]]]\u003e\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":413,"title":"Back to basics 23 - Triangular matrix","description":"Covering some basic topics I haven't seen elsewhere on Cody.\r\n\r\nGiven an input matrix, return a matrix with all elements above and to the right of the main diagonal set to zero. Example:\r\n\r\n  input = [ 1 1 1 1; 1 1 1 1; 1 1 1 1; 1 1 1 1]\r\n  output = [1 0 0 0; 1 1 0 0; 1 1 1 0; 1 1 1 1]\r\n","description_html":"\u003cp\u003eCovering some basic topics I haven't seen elsewhere on Cody.\u003c/p\u003e\u003cp\u003eGiven an input matrix, return a matrix with all elements above and to the right of the main diagonal set to zero. Example:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003einput = [ 1 1 1 1; 1 1 1 1; 1 1 1 1; 1 1 1 1]\r\noutput = [1 0 0 0; 1 1 0 0; 1 1 1 0; 1 1 1 1]\r\n\u003c/pre\u003e","function_template":"function y = triangle(x)\r\n  y = x;\r\nend","test_suite":"%%\r\nx = ones(4,4);\r\ny_correct = [1 0 0 0; 1 1 0 0; 1 1 1 0; 1 1 1 1];\r\nassert(isequal(triangle(x),y_correct))\r\n\r\n%%\r\nx = ones(5,5);\r\ny_correct = [1 0 0 0 0; 1 1 0 0 0; 1 1 1 0 0; 1 1 1 1 0; 1 1 1 1 1];\r\nassert(isequal(triangle(x),y_correct))\r\n\r\n%%\r\nx = reshape(1:16,[4,4]);\r\ny_correct = [1 2 3 4; 0 6 7 8; 0 0 11 12; 0 0 0 16]';\r\nassert(isequal(triangle(x),y_correct))\r\n\r\n%%\r\nx = [0 1 2 3; 0 0 1 2; 0 0 0 1; 0 0 0 0];\r\ny_correct = zeros(4);\r\nassert(isequal(triangle(x),y_correct))","published":true,"deleted":false,"likes_count":7,"comments_count":3,"created_by":1022,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":1123,"test_suite_updated_at":"2017-02-24T16:49:14.000Z","rescore_all_solutions":false,"group_id":19,"created_at":"2012-02-25T21:46:25.000Z","updated_at":"2026-04-03T20:13:43.000Z","published_at":"2012-02-25T21:46:25.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\u003eCovering some basic topics I haven't seen elsewhere on Cody.\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\u003eGiven an input matrix, return a matrix with all elements above and to the right of the main diagonal set to zero. Example:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[input = [ 1 1 1 1; 1 1 1 1; 1 1 1 1; 1 1 1 1]\\noutput = [1 0 0 0; 1 1 0 0; 1 1 1 0; 1 1 1 1]]]\u003e\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":1129,"title":"Reverse the elements of an array","description":"Reverse the order of elements in an array:\r\n\r\neg:\r\n\r\n input X  = [ 1 2 3 ; \r\n              4 5 6 ; \r\n              7 8 9 ]\r\n\r\n output Y = [ 9 8 7 ;\r\n              6 5 4 ;\r\n              3 2 1 ]\r\n","description_html":"\u003cp\u003eReverse the order of elements in an array:\u003c/p\u003e\u003cp\u003eeg:\u003c/p\u003e\u003cpre\u003e input X  = [ 1 2 3 ; \r\n              4 5 6 ; \r\n              7 8 9 ]\u003c/pre\u003e\u003cpre\u003e output Y = [ 9 8 7 ;\r\n              6 5 4 ;\r\n              3 2 1 ]\u003c/pre\u003e","function_template":"function y = your_fcn_name(x)\r\n  y = x;\r\nend","test_suite":"%%\r\nx = [];\r\ny_correct = [];\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n\r\n%%\r\nx = 1;\r\ny_correct = 1;\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n\r\n%%\r\nx = [1:5];\r\ny_correct = [5:-1:1];\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n\r\n%%\r\nx = [1:5;6:10];\r\ny_correct = [10:-1:6;5:-1:1];\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n\r\n%%\r\nx = [1:5;6:10;11:15];\r\ny_correct = [15:-1:11;10:-1:6;5:-1:1];\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n\r\n%%\r\nx = ones(5);\r\ny_correct = ones(5);\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n\r\n%%\r\nx = magic(3);\r\ny_correct = [2 9 4;7 5 3;6 1 8];\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n\r\n%%\r\nx = [2 9 -4;7 -5 3;-6 1 8];\r\ny_correct =[8 1 -6;3 -5 7;-4 9 2] ;\r\nassert(isequal(your_fcn_name(x),y_correct))","published":true,"deleted":false,"likes_count":8,"comments_count":0,"created_by":9317,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":1115,"test_suite_updated_at":"2012-12-18T17:05:19.000Z","rescore_all_solutions":false,"group_id":19,"created_at":"2012-12-18T16:56:00.000Z","updated_at":"2026-03-25T11:14:35.000Z","published_at":"2012-12-18T16:57:22.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\u003eReverse the order of elements in an 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\u003eeg:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ input X  = [ 1 2 3 ; \\n              4 5 6 ; \\n              7 8 9 ]\\n\\n output Y = [ 9 8 7 ;\\n              6 5 4 ;\\n              3 2 1 ]]]\u003e\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":545,"title":"Flip the main diagonal of a matrix","description":"Given a n x n matrix, M, flip its main diagonal.\r\n\r\nExample:\r\n\r\n \u003e\u003e M=magic(5);\r\n\r\n \u003e\u003e flipDiagonal(M)\r\n\r\n     9    24     1     8    15\r\n    23    21     7    14    16\r\n     4     6    13    20    22\r\n    10    12    19     5     3\r\n    11    18    25     2    17\r\n","description_html":"\u003cp\u003eGiven a n x n matrix, M, flip its main diagonal.\u003c/p\u003e\u003cp\u003eExample:\u003c/p\u003e\u003cpre\u003e \u003e\u003e M=magic(5);\u003c/pre\u003e\u003cpre\u003e \u003e\u003e flipDiagonal(M)\u003c/pre\u003e\u003cpre\u003e     9    24     1     8    15\r\n    23    21     7    14    16\r\n     4     6    13    20    22\r\n    10    12    19     5     3\r\n    11    18    25     2    17\u003c/pre\u003e","function_template":"function y = flipDiagonal(x)\r\n  y = x\r\nend","test_suite":"%% test 1\r\nM=magic(5);\r\ny_correct = [9, 24,1,8 ,15; 23 ,21,7,14,16; ...\r\n    4,6,13,20,22; 10,12,19,5,3; 11,18,25,2,17];\r\nassert(isequal(flipDiagonal(M),y_correct))\r\n\r\n%% test 2\r\nM=magic(2);\r\ny_correct=[2 3; 4 1];\r\nassert(isequal(flipDiagonal(M),y_correct))\r\n\r\n%% \r\nM=ones(4);\r\nM(2,2)=4; M(3,3)=9; M(4,4)=16;\r\ny_correct=ones(4);\r\ny_correct(1,1)=16; y_correct(2,2)=9; y_correct(3,3)=4;\r\nassert(isequal(flipDiagonal(M),y_correct))\r\n\r\n%% \r\nM=[1];\r\ny_correct=[1];\r\nassert(isequal(flipDiagonal(M),y_correct))\r\n\r\n%% \r\nM=reshape(1:25,[5,5]);\r\ny_correct=[25 2 3 4 5; 6 19 8 9 10; ...\r\n    11 12 13 14 15; 16 17 18 7 20; 21 22 23 24 1]';\r\nassert(isequal(flipDiagonal(M),y_correct))\r\n\r\n%% \r\nM=[1,1,1,1;2,2,2,2;3,3,3,3;4,4,4,4];\r\ny_correct=[4,1,1,1;2,3,2,2;3,3,2,3;4,4,4,1];\r\nassert(isequal(flipDiagonal(M),y_correct))\r\n","published":true,"deleted":false,"likes_count":7,"comments_count":1,"created_by":1103,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":905,"test_suite_updated_at":"2017-02-24T16:41:36.000Z","rescore_all_solutions":false,"group_id":19,"created_at":"2012-04-01T03:31:45.000Z","updated_at":"2026-03-25T11:36:45.000Z","published_at":"2012-04-04T02:20:41.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 a n x n matrix, M, flip its main diagonal.\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\u003eExample:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ \u003e\u003e M=magic(5);\\n\\n \u003e\u003e flipDiagonal(M)\\n\\n     9    24     1     8    15\\n    23    21     7    14    16\\n     4     6    13    20    22\\n    10    12    19     5     3\\n    11    18    25     2    17]]\u003e\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":794,"title":"Matrix with different incremental runs","description":"Given a vector of positive integers\r\n\r\n  a = [ 3 2 4 ];\r\n\r\ncreate the matrix where the *i* th column contains the vector *1:a(i)* possibly padded\r\nwith zeros:\r\n\r\n  b = [ 1 1 1\r\n        2 2 2\r\n        3 0 3\r\n        0 0 4 ];","description_html":"\u003cp\u003eGiven a vector of positive integers\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003ea = [ 3 2 4 ];\r\n\u003c/pre\u003e\u003cp\u003ecreate the matrix where the \u003cb\u003ei\u003c/b\u003e th column contains the vector \u003cb\u003e1:a(i)\u003c/b\u003e possibly padded\r\nwith zeros:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eb = [ 1 1 1\r\n      2 2 2\r\n      3 0 3\r\n      0 0 4 ];\r\n\u003c/pre\u003e","function_template":"function b = your_fcn_name(a)\r\n  b = a;\r\nend","test_suite":"%%\r\na = [ 3 2 4 ];\r\nb = [ 1 1 1\r\n        2 2 2\r\n        3 0 3\r\n        0 0 4 ];\r\nassert(isequal(your_fcn_name(a),b))\r\n\r\n\r\n%%\r\na = [1 0 8 9 14];\r\nb =[ 1     0     1     1     1\r\n     0     0     2     2     2\r\n     0     0     3     3     3\r\n     0     0     4     4     4\r\n     0     0     5     5     5\r\n     0     0     6     6     6\r\n     0     0     7     7     7\r\n     0     0     8     8     8\r\n     0     0     0     9     9\r\n     0     0     0     0    10\r\n     0     0     0     0    11\r\n     0     0     0     0    12\r\n     0     0     0     0    13\r\n     0     0     0     0    14];\r\nassert(isequal(your_fcn_name(a),b))\r\n\r\n%%\r\n a = [ 4 2 4 ];\r\nb =[ 1     1     1\r\n     2     2     2\r\n     3     0     3\r\n     4     0     4];\r\nassert(isequal(your_fcn_name(a),b))\r\n\r\n%% one value\r\na= 1;\r\n\r\nassert(isequal(your_fcn_name(a),1))\r\n\r\n%% empty vector \r\na = [];\r\n\r\nassert(isequal(your_fcn_name(a),[]))","published":true,"deleted":false,"likes_count":5,"comments_count":0,"created_by":639,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":581,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":19,"created_at":"2012-06-27T09:12:25.000Z","updated_at":"2026-03-15T18:51:31.000Z","published_at":"2012-06-27T09:12:25.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 a vector of positive integers\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[a = [ 3 2 4 ];]]\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:t\u003ecreate the matrix where the\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ei\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e th column contains the vector\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e1:a(i)\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e possibly padded with zeros:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[b = [ 1 1 1\\n      2 2 2\\n      3 0 3\\n      0 0 4 ];]]\u003e\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":611,"title":"surrounded matrix","description":"With a given matrix A (size m x n) create a matrix B (size m+2 x n+2) so that the matrix A is surrounded by ones:\r\n\r\n  A = [1 2 3\r\n       3 2 1]\r\n  \r\n  B = [1 1 1 1 1\r\n       1 1 2 3 1\r\n       1 3 2 1 1\r\n       1 1 1 1 1]\r\n\r\nor\r\n\r\n  A = 4\r\n\r\n  B = [ 1 1 1\r\n        1 4 1\r\n        1 1 1]","description_html":"\u003cp\u003eWith a given matrix A (size m x n) create a matrix B (size m+2 x n+2) so that the matrix A is surrounded by ones:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eA = [1 2 3\r\n     3 2 1]\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003eB = [1 1 1 1 1\r\n     1 1 2 3 1\r\n     1 3 2 1 1\r\n     1 1 1 1 1]\r\n\u003c/pre\u003e\u003cp\u003eor\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eA = 4\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003eB = [ 1 1 1\r\n      1 4 1\r\n      1 1 1]\r\n\u003c/pre\u003e","function_template":"function B = surrounded(A)\r\n  B = A;\r\nend","test_suite":"%%\r\nx = 1;\r\ny_correct = ones(3);\r\nassert(isequal(surrounded(x),y_correct))\r\n\r\n%%\r\nx = [3 3 3;3 3 3];\r\ny_correct = [1 1 1 1 1;1 3 3 3 1; 1 3 3 3 1; 1 1 1 1 1];\r\nassert(isequal(surrounded(x),y_correct))\r\n\r\n%%\r\nx = ones(3,4);\r\ny_correct = ones(5,6);\r\nassert(isequal(surrounded(x),y_correct))\r\n\r\n%%\r\nx = [0];\r\ny_correct = ones(3); y_correct(2,2) = 0;\r\nassert(isequal(surrounded(x),y_correct))\r\n\r\n%%\r\nx = reshape(1:9,[3,3]);\r\ny_correct = [1 1 1 1 1; 1 1 2 3 1; ...\r\n    1 4 5 6 1; 1 7 8 9 1; 1 1 1 1 1]';\r\nassert(isequal(surrounded(x),y_correct))\r\n","published":true,"deleted":false,"likes_count":6,"comments_count":2,"created_by":3062,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":1453,"test_suite_updated_at":"2017-02-24T16:54:28.000Z","rescore_all_solutions":false,"group_id":19,"created_at":"2012-04-22T09:21:22.000Z","updated_at":"2026-04-02T19:15:31.000Z","published_at":"2012-04-22T09:24:58.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\u003eWith a given matrix A (size m x n) create a matrix B (size m+2 x n+2) so that the matrix A is surrounded by ones:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[A = [1 2 3\\n     3 2 1]\\n\\nB = [1 1 1 1 1\\n     1 1 2 3 1\\n     1 3 2 1 1\\n     1 1 1 1 1]]]\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:t\u003eor\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[A = 4\\n\\nB = [ 1 1 1\\n      1 4 1\\n      1 1 1]]]\u003e\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":497,"title":"Make an awesome ramp for a tiny motorcycle stuntman","description":"Okay, given a vector, say v=[1 3 6 9 11], turn it into a matrix 'ramp' like so:\r\n\r\n  m=[1 3 6 9 11;\r\n     3 6 9 11 0;\r\n     6 9 11 0 0;\r\n     9 11 0 0 0;\r\n     11 0 0 0 0]\r\n\r\nThat way a tiny motorcycle stuntman can follow the trace and jump over 9 school buses safely.","description_html":"\u003cp\u003eOkay, given a vector, say v=[1 3 6 9 11], turn it into a matrix 'ramp' like so:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003em=[1 3 6 9 11;\r\n   3 6 9 11 0;\r\n   6 9 11 0 0;\r\n   9 11 0 0 0;\r\n   11 0 0 0 0]\r\n\u003c/pre\u003e\u003cp\u003eThat way a tiny motorcycle stuntman can follow the trace and jump over 9 school buses safely.\u003c/p\u003e","function_template":"function ramp = vrooom(v)\r\n  ramp = zeros(numel(v));\r\nend","test_suite":"%%\r\nv = [1 3 6 9 11];\r\ny_correct = [1 3 6 9 11;\r\n   3 6 9 11 0;\r\n   6 9 11 0 0;\r\n   9 11 0 0 0;\r\n   11 0 0 0 0];\r\nassert(isequal(vrooom(v),y_correct))\r\n\r\n%%\r\nv = [1 2 3];\r\ny_correct = [1 2 3;\r\n   2 3 0;\r\n   3 0 0];\r\nassert(isequal(vrooom(v),y_correct))\r\n\r\n%%\r\nv = [-1 0 2 8];\r\ny_correct = [-1 0 2 8;\r\n0 2 8 0;\r\n2 8 0 0;\r\n8 0 0 0];\r\nassert(isequal(vrooom(v),y_correct))\r\n","published":true,"deleted":false,"likes_count":6,"comments_count":3,"created_by":1675,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":750,"test_suite_updated_at":"2012-03-15T04:23:51.000Z","rescore_all_solutions":false,"group_id":19,"created_at":"2012-03-15T04:13:09.000Z","updated_at":"2026-03-15T18:45:42.000Z","published_at":"2012-03-15T21:15:55.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\u003eOkay, given a vector, say v=[1 3 6 9 11], turn it into a matrix 'ramp' like so:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[m=[1 3 6 9 11;\\n   3 6 9 11 0;\\n   6 9 11 0 0;\\n   9 11 0 0 0;\\n   11 0 0 0 0]]]\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:t\u003eThat way a tiny motorcycle stuntman can follow the trace and jump over 9 school buses safely.\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":300,"title":"Remove NaN ?","description":"input -\u003e matrix (n*m) with  at least one element equal to NaN;\r\noutput -\u003e matrix(p*m), the same matrix where we deleted the entire line containing NaN;","description_html":"\u003cp\u003einput -\u003e matrix (n*m) with  at least one element equal to NaN;\r\noutput -\u003e matrix(p*m), the same matrix where we deleted the entire line containing NaN;\u003c/p\u003e","function_template":"function y = removeNaN(x)\r\n  y = x;\r\nend","test_suite":"%%\r\nx = magic(3);x(2,[1 3])= NaN;\r\ny_correct = [8 1 6;4 9 2];\r\nassert(isequal(removeNaN(x),y_correct))\r\n\r\n%%\r\nx = [magic(4) ones(4,1)];\r\nx(4,5) = NaN;x(3,2) = Inf;x(3,1)=NaN;\r\ny_correct = x(1:2,:);\r\nassert(isequal(removeNaN(x),y_correct))\r\n\r\n%%\r\nx = ones(4);x(3,3)=NaN;\r\ny_correct = ones(3,4);\r\nassert(isequal(removeNaN(x),y_correct))\r\n\r\n%%\r\nx = [1; NaN];\r\ny_correct = [1];\r\nassert(isequal(removeNaN(x),y_correct))\r\n\r\n%%\r\nx = NaN;\r\nassert(isempty(removeNaN(x)))\r\n\r\n%%\r\nx = [1:10 NaN];\r\nassert(isempty(removeNaN(x)))\r\n\r\n%%\r\nx = [1:10 NaN]';\r\ny_correct = [1:10]';\r\nassert(isequal(removeNaN(x),y_correct))\r\n","published":true,"deleted":false,"likes_count":13,"comments_count":3,"created_by":1309,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":1354,"test_suite_updated_at":"2017-02-24T16:46:34.000Z","rescore_all_solutions":false,"group_id":19,"created_at":"2012-02-09T22:17:06.000Z","updated_at":"2026-04-02T15:06:03.000Z","published_at":"2012-02-09T23:49:31.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\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"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\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\u003einput -\u003e matrix (n*m) with at least one element equal to NaN; output -\u003e matrix(p*m), the same matrix where we deleted the entire line containing NaN;\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":798,"title":"Rotate input square matrix 90 degrees CCW without rot90","description":"Rotate input matrix (which will be square) 90 degrees counter-clockwise without using rot90,flipud,fliplr, or flipdim (or eval).  \r\n\r\nExample input: [ 1 2 3; 4 5 6; 7 8 9]\r\n\r\nExample output: [ 3 6 9; 2 5 8; 1 4 7]\r\n\r\nBecause of simplistic regexp tests (sorry), you shouldn't use variable names or strings composed of the outlawed function names.","description_html":"\u003cp\u003eRotate input matrix (which will be square) 90 degrees counter-clockwise without using rot90,flipud,fliplr, or flipdim (or eval).\u003c/p\u003e\u003cp\u003eExample input: [ 1 2 3; 4 5 6; 7 8 9]\u003c/p\u003e\u003cp\u003eExample output: [ 3 6 9; 2 5 8; 1 4 7]\u003c/p\u003e\u003cp\u003eBecause of simplistic regexp tests (sorry), you shouldn't use variable names or strings composed of the outlawed function names.\u003c/p\u003e","function_template":"function y = rot_hardway(x)\r\n  y = x;\r\nend","test_suite":"%%\r\nassert(isempty(regexp(evalc('type rot_hardway'),'(eval|fliplr|flipud|flipdim|rot90|)')))\r\n\r\n%%\r\nx = [1 2 3;4 5 6;7 8 9];\r\ny_correct = [3 6 9;2 5 8;1 4 7];\r\nassert(isequal(rot_hardway(x),y_correct))\r\n\r\n%%\r\nx = [sqrt(-1) 2 3;4 5 6;7 8 9];\r\ny_correct = [3 6 9;2 5 8;sqrt(-1) 4 7];\r\nassert(isequal(rot_hardway(x),y_correct))\r\n\r\n%%\r\nx = magic(9);\r\ny_correct = rot90(magic(9));\r\nassert(isequal(rot_hardway(x),y_correct))\r\n","published":true,"deleted":false,"likes_count":5,"comments_count":3,"created_by":2846,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":683,"test_suite_updated_at":"2017-02-24T16:59:54.000Z","rescore_all_solutions":false,"group_id":19,"created_at":"2012-06-27T19:21:15.000Z","updated_at":"2026-03-15T18:53:31.000Z","published_at":"2012-06-27T19:21:15.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\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"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\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\u003eRotate input matrix (which will be square) 90 degrees counter-clockwise without using rot90,flipud,fliplr, or flipdim (or eval).\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExample input: [ 1 2 3; 4 5 6; 7 8 9]\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExample output: [ 3 6 9; 2 5 8; 1 4 7]\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eBecause of simplistic regexp tests (sorry), you shouldn't use variable names or strings composed of the outlawed function names.\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":858,"title":"Permute diagonal and antidiagonal","description":"Permute diagonal and antidiagonal\r\nFor example [1 2 3;4 5 6;7 8 9] -\u003e [3 2 1;4 5 6;9 8 7]\r\nWITHOUT diag function (and variable name)\r\n","description_html":"\u003cp\u003ePermute diagonal and antidiagonal\r\nFor example [1 2 3;4 5 6;7 8 9] -\u003e [3 2 1;4 5 6;9 8 7]\r\nWITHOUT diag function (and variable name)\u003c/p\u003e","function_template":"function y = permuted(x)\r\n  y = withoutdiag(x);\r\nend","test_suite":"%%\r\nx = [1 2 3;4 5 6;7 8 9];\r\ny_correct = [3 2 1;4 5 6;9 8 7];\r\nfiletext = fileread('permuted.m');\r\nassert(isequal(permuted(x),y_correct))\r\nassert(isempty(strfind(filetext, 'diag')))\r\n%%\r\nx=[1 2;3 4];\r\ny_correct = [2 1;4 3];\r\nfiletext = fileread('permuted.m');\r\nassert(isequal(permuted(x),y_correct))\r\nassert(isempty(strfind(filetext, 'diag')))\r\n%%\r\nx=[1 2;3 4];\r\ny_correct = [2 1;4 3];\r\nfiletext = fileread('permuted.m');\r\nassert(isequal(permuted(x),y_correct))\r\nassert(isempty(strfind(filetext, 'diag')))\r\n%%\r\nx=magic(10);\r\ny_correct = [    40    99     1     8    15    67    74    51    58    92\r\n    98    64     7    14    16    73    55    57    80    41\r\n     4    81    63    20    22    54    56    88    70    47\r\n    85    87    19    62     3    60    21    69    71    28\r\n    86    93    25     2    61     9    68    75    52    34\r\n    17    24    76    83    42    90    49    26    33    65\r\n    23     5    82    30    91    48    89    32    39    66\r\n    79     6    38    95    97    29    31    13    45    72\r\n    10    46    94    96    78    35    37    44    12    53\r\n    59    18   100    77    84    36    43    50    27    11];\r\nfiletext = fileread('permuted.m');\r\nassert(isequal(permuted(x),y_correct))\r\nassert(isempty(strfind(filetext, 'diag')))\r\n\r\n\r\n\r\n","published":true,"deleted":false,"likes_count":7,"comments_count":1,"created_by":5390,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":505,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":19,"created_at":"2012-07-24T01:19:14.000Z","updated_at":"2026-04-02T19:11:56.000Z","published_at":"2012-07-24T01:19:14.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\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"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\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\u003ePermute diagonal and antidiagonal For example [1 2 3;4 5 6;7 8 9] -\u003e [3 2 1;4 5 6;9 8 7] WITHOUT diag function (and variable name)\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":1042,"title":"Operate on matrices of unequal, yet similar, size","description":"You may want to add a vector to a matrix, implying that the vector is added to each column of the matrix. Or multiply a 3x4x5 matrix with a 3x4 matrix, intending to do that for every element in the 3th dimension. \r\n\r\n  \u003e\u003e [1 2;3 4].*[1 2]\r\n  \r\n  ans =\r\n  \r\n     1     4\r\n     3     8\r\n\r\nIt's clear what you want to do, but Matlab is very strict and gives an error, unless you first make the size of the arguments match.\r\n\r\nYou will now create a function that operates on two arguments, performing one of the operations 'plus', 'minus', 'times', 'power', 'ldivide' or 'rdivide' (specified as the 3rd argument), after it has matched the sizes of first two input arguments.\r\n\r\nIt should work on input arguments of more than two dimensions, of arguments of different dimensions, as long as they are numerical matrices, vectors, or values.\r\n\r\nThere is one limitation: for dimensions in which the size of the input arguments differ, the size of one of them should be one.\r\n\r\nNote: Solutions wrapped in eval(c), inline, str2func, regexprep (dynamic regular expressions), etc, are not appreciated.","description_html":"\u003cp\u003eYou may want to add a vector to a matrix, implying that the vector is added to each column of the matrix. Or multiply a 3x4x5 matrix with a 3x4 matrix, intending to do that for every element in the 3th dimension.\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e\u003e\u003e [1 2;3 4].*[1 2]\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003eans =\r\n\u003c/pre\u003e\u003cpre\u003e     1     4\r\n     3     8\u003c/pre\u003e\u003cp\u003eIt's clear what you want to do, but Matlab is very strict and gives an error, unless you first make the size of the arguments match.\u003c/p\u003e\u003cp\u003eYou will now create a function that operates on two arguments, performing one of the operations 'plus', 'minus', 'times', 'power', 'ldivide' or 'rdivide' (specified as the 3rd argument), after it has matched the sizes of first two input arguments.\u003c/p\u003e\u003cp\u003eIt should work on input arguments of more than two dimensions, of arguments of different dimensions, as long as they are numerical matrices, vectors, or values.\u003c/p\u003e\u003cp\u003eThere is one limitation: for dimensions in which the size of the input arguments differ, the size of one of them should be one.\u003c/p\u003e\u003cp\u003eNote: Solutions wrapped in eval(c), inline, str2func, regexprep (dynamic regular expressions), etc, are not appreciated.\u003c/p\u003e","function_template":"function z = matched_op(x,y,op)\r\n  z = feval(op,x,y);\r\nend","test_suite":"%%\r\nnocheat = isempty(regexp(evalc('type matched_op'),'([^f]eval|regexprep|inline|str2func)'));\r\nx = [1 2;3 4];\r\ny = [1 2];\r\nz_correct = [1 4;3 8];\r\nassert(isequal(matched_op(x,y,'times'),z_correct) \u0026\u0026 nocheat)\r\n\r\n%%\r\nnocheat = isempty(regexp(evalc('type matched_op'),'([^f]eval|regexprep|inline|str2func)'));\r\nx = [10i];\r\ny = [20];\r\nz_correct = [-2i];\r\nassert(isequal(matched_op(x,y,'ldivide'),z_correct) \u0026\u0026 nocheat)\r\n\r\n%%\r\nnocheat = isempty(regexp(evalc('type matched_op'),'([^f]eval|regexprep|inline|str2func)'));\r\nx = reshape(1:4,[1 1 1 4]);\r\ny = (1:4).';\r\nz_correct = reshape([2 3 4 5 3 4 5 6 4 5 6 7 5 6 7 8],[4 1 1 4]);\r\nassert(isequal(matched_op(x,y,'plus'),z_correct) \u0026\u0026 nocheat)\r\n\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":3,"created_by":6556,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":229,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":19,"created_at":"2012-11-20T09:59:22.000Z","updated_at":"2026-03-15T18:54:46.000Z","published_at":"2012-11-20T09:59:31.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\u003eYou may want to add a vector to a matrix, implying that the vector is added to each column of the matrix. Or multiply a 3x4x5 matrix with a 3x4 matrix, intending to do that for every element in the 3th dimension.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[\u003e\u003e [1 2;3 4].*[1 2]\\n\\nans =\\n\\n     1     4\\n     3     8]]\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:t\u003eIt's clear what you want to do, but Matlab is very strict and gives an error, unless you first make the size of the arguments match.\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\u003eYou will now create a function that operates on two arguments, performing one of the operations 'plus', 'minus', 'times', 'power', 'ldivide' or 'rdivide' (specified as the 3rd argument), after it has matched the sizes of first two input arguments.\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\u003eIt should work on input arguments of more than two dimensions, of arguments of different dimensions, as long as they are numerical matrices, vectors, or 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:t\u003eThere is one limitation: for dimensions in which the size of the input arguments differ, the size of one of them should be one.\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: Solutions wrapped in eval(c), inline, str2func, regexprep (dynamic regular expressions), etc, are not appreciated.\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":792,"title":"Set some matrix elements to zero","description":"First get the maximum of each *row*, and afterwards set all the other elements to zero. For example, this matrix:\r\n\r\n  1 \t2 \t3 \t4\r\n  5 \t5 \t6 \t5\r\n  7 \t9 \t8 \t3\r\n  \r\nshould become:\r\n\r\n  0 \t0 \t0 \t4\r\n  0 \t0 \t6 \t0\r\n  0 \t9 \t0 \t0\r\n\r\nInput will never be empty.\r\n\r\nIf a row has the same maximum in different columns , display the first occurrence:\r\n\r\nIn the below example, the second row has twice the number 8 as maximum , the output will display the first 8 (column2) :\r\n\r\n   x =\r\n  \r\n       5     4     5\r\n       2     8     8\r\n\r\n\r\n   should become :\r\n\r\n     \r\n     5     0     0\r\n     0     8     0\r\n","description_html":"\u003cp\u003eFirst get the maximum of each \u003cb\u003erow\u003c/b\u003e, and afterwards set all the other elements to zero. For example, this matrix:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e1 \t2 \t3 \t4\r\n5 \t5 \t6 \t5\r\n7 \t9 \t8 \t3\r\n\u003c/pre\u003e\u003cp\u003eshould become:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e0 \t0 \t0 \t4\r\n0 \t0 \t6 \t0\r\n0 \t9 \t0 \t0\r\n\u003c/pre\u003e\u003cp\u003eInput will never be empty.\u003c/p\u003e\u003cp\u003eIf a row has the same maximum in different columns , display the first occurrence:\u003c/p\u003e\u003cp\u003eIn the below example, the second row has twice the number 8 as maximum , the output will display the first 8 (column2) :\u003c/p\u003e\u003cpre\u003e   x =\u003c/pre\u003e\u003cpre\u003e       5     4     5\r\n       2     8     8\u003c/pre\u003e\u003cpre\u003e   should become :\u003c/pre\u003e\u003cpre\u003e     5     0     0\r\n     0     8     0\u003c/pre\u003e","function_template":"function y = your_fcn_name(x)\r\n  y = x;\r\nend","test_suite":"%%\r\nx = [1     2     3     4\r\n     5     5     6     5\r\n     7     9     8     3];\r\ny_correct = [0 \t0 0 4\r\n             0 \t0 6 0\r\n             0 \t9 0 0];\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n\r\n%% \r\nx = magic(4);\r\ny_correct  = [16     0     0     0\r\n     0    11     0     0\r\n     0     0     0    12\r\n     0     0    15     0];\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n\r\n%% \r\nx = pi;\r\ny_correct  = pi;\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n\r\n%% \r\nx = 0;\r\ny_correct  = 0;\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n\r\n%%\r\n x = toeplitz(1:5);\r\ny_correct  = [   0     0     0     0     5\r\n     0     0     0     0     4\r\n     3     0     0     0     0\r\n     4     0     0     0     0\r\n     5     0     0     0     0];\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n\r\n%% \r\nx = ones(5);\r\ny_correct  = [ 1     0     0     0     0\r\n     1     0     0     0     0\r\n     1     0     0     0     0\r\n     1     0     0     0     0\r\n     1     0     0     0     0];\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n\r\n%%\r\nx =nchoosek(7:9,3);\r\ny_correct  =[ 0     0     9];\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n\r\n%%\r\nx =nchoosek(4:9,2);\r\ny_correct  =  [ 0     5\r\n     0     6\r\n     0     7\r\n     0     8\r\n     0     9\r\n     0     6\r\n     0     7\r\n     0     8\r\n     0     9\r\n     0     7\r\n     0     8\r\n     0     9\r\n     0     8\r\n     0     9\r\n     0     9];\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n","published":true,"deleted":false,"likes_count":8,"comments_count":1,"created_by":639,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":626,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":19,"created_at":"2012-06-27T07:35:57.000Z","updated_at":"2026-03-28T02:33:55.000Z","published_at":"2012-06-27T07:35: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\u003eFirst get the maximum of each\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003erow\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, and afterwards set all the other elements to zero. For example, this matrix:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[1   2   3   4\\n5   5   6   5\\n7   9   8   3]]\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:t\u003eshould become:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[0   0   0   4\\n0   0   6   0\\n0   9   0   0]]\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:t\u003eInput will never be empty.\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\u003eIf a row has the same maximum in different columns , display the first occurrence:\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\u003eIn the below example, the second row has twice the number 8 as maximum , the output will display the first 8 (column2) :\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[   x =\\n\\n       5     4     5\\n       2     8     8\\n\\n   should become :\\n\\n     5     0     0\\n     0     8     0]]\u003e\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":796,"title":"Removing rows from a matrix is easy - but what about inserting rows?","description":"Assume A is a 5-by-5 matrix. A([2,4],:) = [] is a quick way to remove rows 2 and 4. Can you find a quick way to insert rows into A?\r\n\r\nYou are given a n-by-m matrix A, a 1-by-p vector of indices IND, and either a scalar value (1-by-1) or a p-by-m matrix b to be inserted AFTER rows IND of A. In case of a scalar value it should fill the whole row. Note that IND may contain duplicate indices, which means that more than one row should be inserted after that specific row index.\r\n\r\nFor example:\r\n\r\n   A = [1 1; \r\n        3 3; \r\n        4 4];\r\n   IND = [1 3];\r\n   b = [2 2; \r\n        5 5];\r\n\r\nshould become\r\n\r\n   y = [1 1; \r\n        2 2; \r\n        3 3; \r\n        4 4; \r\n        5 5];\r\n\r\nAnd\r\n\r\n   A = [0 0; \r\n        1 1];\r\n   IND = [1 1 2];\r\n   b = NaN;\r\n\r\nshould become\r\n\r\n   y = [0   0; \r\n        NaN NaN; \r\n        NaN NaN; \r\n        1 1; \r\n        NaN NaN];\r\n","description_html":"\u003cp\u003eAssume A is a 5-by-5 matrix. A([2,4],:) = [] is a quick way to remove rows 2 and 4. Can you find a quick way to insert rows into A?\u003c/p\u003e\u003cp\u003eYou are given a n-by-m matrix A, a 1-by-p vector of indices IND, and either a scalar value (1-by-1) or a p-by-m matrix b to be inserted AFTER rows IND of A. In case of a scalar value it should fill the whole row. Note that IND may contain duplicate indices, which means that more than one row should be inserted after that specific row index.\u003c/p\u003e\u003cp\u003eFor example:\u003c/p\u003e\u003cpre\u003e   A = [1 1; \r\n        3 3; \r\n        4 4];\r\n   IND = [1 3];\r\n   b = [2 2; \r\n        5 5];\u003c/pre\u003e\u003cp\u003eshould become\u003c/p\u003e\u003cpre\u003e   y = [1 1; \r\n        2 2; \r\n        3 3; \r\n        4 4; \r\n        5 5];\u003c/pre\u003e\u003cp\u003eAnd\u003c/p\u003e\u003cpre\u003e   A = [0 0; \r\n        1 1];\r\n   IND = [1 1 2];\r\n   b = NaN;\u003c/pre\u003e\u003cp\u003eshould become\u003c/p\u003e\u003cpre\u003e   y = [0   0; \r\n        NaN NaN; \r\n        NaN NaN; \r\n        1 1; \r\n        NaN NaN];\u003c/pre\u003e","function_template":"function y = insertRows(A,IND,b)\r\n  y = x;\r\nend","test_suite":"%%\r\nA = 1;\r\nIND = 1;\r\nb = 1;\r\ny_correct = [1;1];\r\nassert(isequal(insertRows(A, IND, b), y_correct))\r\n\r\n%%\r\nA = [0 0; 1 1];\r\nIND = [1 1 1 2];\r\nb = NaN;\r\ny_correct = [0 0;NaN NaN;NaN NaN;NaN NaN;1 1;NaN NaN];\r\nassert(isequalwithequalnans(insertRows(A, IND, b), y_correct))\r\n\r\n%%\r\nA = [1 1; 3 3; 4 4];\r\nIND = [1 3];\r\nb = [2 2; 5 5];\r\ny_correct = [1 1;2 2;3 3;4 4;5 5];\r\nassert(isequal(insertRows(A, IND, b), y_correct))\r\n\r\n%%\r\nA = (1:2:10)';\r\nIND = 1:5;\r\nb = (2:2:10)';\r\ny_correct = (1:10)';\r\nassert(isequal(insertRows(A, IND, b), y_correct))\r\n\r\n%%\r\nA = zeros(10,5);\r\nIND = [3,7];\r\nb = rand(2,5);\r\ny_correct = [zeros(3,5); b(1,:); zeros(4,5); b(2,:); zeros(3,5)];\r\nassert(isequal(insertRows(A, IND, b), y_correct))\r\n\r\n%%\r\nassert(isempty(strfind(evalc('type insertRows'), 'regexp')));","published":true,"deleted":false,"likes_count":3,"comments_count":4,"created_by":4976,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":265,"test_suite_updated_at":"2013-10-20T11:51:38.000Z","rescore_all_solutions":false,"group_id":19,"created_at":"2012-06-27T17:17:54.000Z","updated_at":"2026-03-15T18:52:34.000Z","published_at":"2012-06-27T17:25:07.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\u003eAssume A is a 5-by-5 matrix. A([2,4],:) = [] is a quick way to remove rows 2 and 4. Can you find a quick way to insert rows into A?\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\u003eYou are given a n-by-m matrix A, a 1-by-p vector of indices IND, and either a scalar value (1-by-1) or a p-by-m matrix b to be inserted AFTER rows IND of A. In case of a scalar value it should fill the whole row. Note that IND may contain duplicate indices, which means that more than one row should be inserted after that specific row index.\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\u003eFor example:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[   A = [1 1; \\n        3 3; \\n        4 4];\\n   IND = [1 3];\\n   b = [2 2; \\n        5 5];]]\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:t\u003eshould become\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[   y = [1 1; \\n        2 2; \\n        3 3; \\n        4 4; \\n        5 5];]]\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:t\u003eAnd\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[   A = [0 0; \\n        1 1];\\n   IND = [1 1 2];\\n   b = NaN;]]\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:t\u003eshould become\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[   y = [0   0; \\n        NaN NaN; \\n        NaN NaN; \\n        1 1; \\n        NaN NaN];]]\u003e\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":112,"title":"Remove the air bubbles","description":"Given a matrix a, return a matrix b in which all the zeros have \"bubbled\" to the top. That is, any zeros in a given column should be moved to the top. The order of the remaining nonzero numbers in the column should be preserved.\r\n\r\nExample 1\r\n\r\n Input  a  = [ 1 2 3\r\n               0 4 5\r\n               6 0 0 ]\r\n  \r\n Output b is [ 0 0 0\r\n               1 2 3\r\n               6 4 5 ]\r\n\r\nExample 2\r\n\r\n Input  a  = [ 1 0 5 0 6 0 7 ]'\r\n Output b is [ 0 0 0 1 5 6 7 ]'\r\n\r\nExample 3\r\n\r\n Input  a  = [ 1 0\r\n               1 1 ]\r\n Output b is [ 1 0 \r\n               1 1 ]\r\n","description_html":"\u003cp\u003eGiven a matrix a, return a matrix b in which all the zeros have \"bubbled\" to the top. That is, any zeros in a given column should be moved to the top. The order of the remaining nonzero numbers in the column should be preserved.\u003c/p\u003e\u003cp\u003eExample 1\u003c/p\u003e\u003cpre\u003e Input  a  = [ 1 2 3\r\n               0 4 5\r\n               6 0 0 ]\u003c/pre\u003e\u003cpre\u003e Output b is [ 0 0 0\r\n               1 2 3\r\n               6 4 5 ]\u003c/pre\u003e\u003cp\u003eExample 2\u003c/p\u003e\u003cpre\u003e Input  a  = [ 1 0 5 0 6 0 7 ]'\r\n Output b is [ 0 0 0 1 5 6 7 ]'\u003c/pre\u003e\u003cp\u003eExample 3\u003c/p\u003e\u003cpre\u003e Input  a  = [ 1 0\r\n               1 1 ]\r\n Output b is [ 1 0 \r\n               1 1 ]\u003c/pre\u003e","function_template":"function b = bubbles(a)\r\n  b = [];\r\nend","test_suite":"%%\r\n\r\na = [ 1 2 3\r\n      0 4 5\r\n      6 0 0 ];\r\nb_correct = [ 0 0 0\r\n       1 2 3\r\n       6 4 5 ];\r\nassert(isequal(bubbles(a),b_correct))\r\n\r\n%%\r\n\r\na = [ 1 0 5 0 7 0 6 ]';\r\nb_correct = [ 0 0 0 1 5 7 6 ]';\r\nassert(isequal(bubbles(a),b_correct))\r\n\r\n%%\r\n\r\na = [1 0; 1 1];\r\nb_correct = [1 0; 1 1];\r\nassert(isequal(bubbles(a),b_correct))\r\n\r\n%%\r\n\r\na = [0 8 0 6 -2]';\r\nb_correct = [0 0 8 6 -2]';\r\nassert(isequal(bubbles(a),b_correct))\r\n","published":true,"deleted":false,"likes_count":21,"comments_count":5,"created_by":7,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":1332,"test_suite_updated_at":"2012-01-29T23:46:46.000Z","rescore_all_solutions":false,"group_id":19,"created_at":"2012-01-26T20:27:23.000Z","updated_at":"2026-04-03T20:05:10.000Z","published_at":"2012-01-29T23:54:21.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 a matrix a, return a matrix b in which all the zeros have \\\"bubbled\\\" to the top. That is, any zeros in a given column should be moved to the top. The order of the remaining nonzero numbers in the column should be preserved.\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\u003eExample 1\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ Input  a  = [ 1 2 3\\n               0 4 5\\n               6 0 0 ]\\n\\n Output b is [ 0 0 0\\n               1 2 3\\n               6 4 5 ]]]\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:t\u003eExample 2\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ Input  a  = [ 1 0 5 0 6 0 7 ]'\\n Output b is [ 0 0 0 1 5 6 7 ]']]\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:t\u003eExample 3\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ Input  a  = [ 1 0\\n               1 1 ]\\n Output b is [ 1 0 \\n               1 1 ]]]\u003e\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":375,"title":"N-Dimensional Array Slice","description":"Given an N-dimensional array, _A_, an index, _I_, and a dimension, _d_, return the _I_ th elements of _A_ in the _d_ dimension.\r\n\r\nFor Example,\r\n\r\n    array_slice( A, 5, 3 )\r\n\r\nis equivalent to\r\n\r\n    A(:,:,5)\r\n\r\nNote: |eval| and |str2func| cannot be used. This is a Cody restriction.","description_html":"\u003cp\u003eGiven an N-dimensional array, \u003ci\u003eA\u003c/i\u003e, an index, \u003ci\u003eI\u003c/i\u003e, and a dimension, \u003ci\u003ed\u003c/i\u003e, return the \u003ci\u003eI\u003c/i\u003e th elements of \u003ci\u003eA\u003c/i\u003e in the \u003ci\u003ed\u003c/i\u003e dimension.\u003c/p\u003e\u003cp\u003eFor Example,\u003c/p\u003e\u003cpre\u003e    array_slice( A, 5, 3 )\u003c/pre\u003e\u003cp\u003eis equivalent to\u003c/p\u003e\u003cpre\u003e    A(:,:,5)\u003c/pre\u003e\u003cp\u003eNote: \u003ctt\u003eeval\u003c/tt\u003e and \u003ctt\u003estr2func\u003c/tt\u003e cannot be used. This is a Cody restriction.\u003c/p\u003e","function_template":"function S = arraySlice(A,I,d)\r\n  S = A(:,I);\r\nend","test_suite":"%%\r\nA = randn(5,5);\r\nassert(isequal(arraySlice(A,4,2),A(:,4)))\r\n\r\n%%\r\nA = randn(5,5);\r\nassert(isequal(arraySlice(A,4,1),A(4,:)))\r\n\r\n%%\r\nA = randn(5,5);\r\nassert(isequal(arraySlice(A,1,10),A))\r\n\r\n%%\r\nA = randn(5,5,5,3);\r\nassert(isequal(arraySlice(A,3,4),A(:,:,:,3)))\r\n\r\n%%\r\nA = randn(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2);\r\nassert(isequal(arraySlice(A,2,18),A(:,:,:,:,:,:,:,:,:,:,:,:,:,:,:,:,:,2)))","published":true,"deleted":false,"likes_count":13,"comments_count":7,"created_by":134,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":284,"test_suite_updated_at":"2012-02-21T16:23:06.000Z","rescore_all_solutions":false,"group_id":19,"created_at":"2012-02-21T16:23:06.000Z","updated_at":"2026-04-01T12:13:28.000Z","published_at":"2012-02-21T16:23: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\u003eGiven an N-dimensional array,\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eA\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, an index,\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eI\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, and a dimension,\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ed\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, return the\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eI\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e th elements of\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eA\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e in the\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ed\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e dimension.\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\u003eFor Example,\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[    array_slice( A, 5, 3 )]]\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:t\u003eis equivalent to\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[    A(:,:,5)]]\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:t\u003eNote:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eeval\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e and\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003estr2func\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e cannot be used. This is a Cody restriction.\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":782,"title":"Some Assembly Required","description":"The input to this function is a matrix of real numbers.  Your job is to assemble the rows of the matrix into one large row that contains all of the individual rows of the matrix, and to make this output row as short as possible.  You accomplish this by joining the rows together at the points at which they overlap.  To help with the task, you can flip rows if you need to do so.\r\n\r\nFor example:\r\n\r\n  input  = [1 1 2 3 ; 4 5 6 7 ; 5 4 8 3]\r\n\r\n  output = [1 1 2 3 8 4 5 6 7]\r\n\r\nExplanation:\r\nThe [1 1 2 3] is the first row of the input.\r\n\r\n[3 8 4 5] is the third row (flipped) so the 3 at the end of the third row overlaps with the 3 at the end of the first row.\r\n\r\n[4 5 6 7] is the second row, with the [4 5] overlapping with the flipped [5 4] from the third row.\r\n\r\nOther than the mirrored version of the solution ([7 6 5 4 8 3 2 1 1] in the above example), each solution will be unique.  Flipped versions of the entered value of y_correct will be tested for as well, in case your code comes up with that version of the correct answer.\r\n\r\nGood luck, and happy hunting.","description_html":"\u003cp\u003eThe input to this function is a matrix of real numbers.  Your job is to assemble the rows of the matrix into one large row that contains all of the individual rows of the matrix, and to make this output row as short as possible.  You accomplish this by joining the rows together at the points at which they overlap.  To help with the task, you can flip rows if you need to do so.\u003c/p\u003e\u003cp\u003eFor example:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003einput  = [1 1 2 3 ; 4 5 6 7 ; 5 4 8 3]\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003eoutput = [1 1 2 3 8 4 5 6 7]\r\n\u003c/pre\u003e\u003cp\u003eExplanation:\r\nThe [1 1 2 3] is the first row of the input.\u003c/p\u003e\u003cp\u003e[3 8 4 5] is the third row (flipped) so the 3 at the end of the third row overlaps with the 3 at the end of the first row.\u003c/p\u003e\u003cp\u003e[4 5 6 7] is the second row, with the [4 5] overlapping with the flipped [5 4] from the third row.\u003c/p\u003e\u003cp\u003eOther than the mirrored version of the solution ([7 6 5 4 8 3 2 1 1] in the above example), each solution will be unique.  Flipped versions of the entered value of y_correct will be tested for as well, in case your code comes up with that version of the correct answer.\u003c/p\u003e\u003cp\u003eGood luck, and happy hunting.\u003c/p\u003e","function_template":"function y = assemble_this(x)\r\n  y = x;\r\nend","test_suite":"%%\r\nx= [1 2 ; 2 3 ; 3 4 ; 4 5];\r\ny_correct=[1 2 3 4 5];\r\nassert(isequal(assemble_this(x),y_correct)||isequal(assemble_this(x),fliplr(y_correct)))\r\n%%\r\nx= [1 1 2 3 ; 4 5 6 7 ; 5 4 8 3];\r\ny_correct=[1 1 2 3 8 4 5 6 7];\r\nassert(isequal(assemble_this(x),y_correct)||isequal(assemble_this(x),fliplr(y_correct)))\r\n%%\r\nx=[2 3 ; 4 2 ; 3 1 ; 1 5 ; 5 9];\r\ny_correct=[9 5 1 3 2 4];\r\nassert(isequal(assemble_this(x),y_correct)||isequal(assemble_this(x),fliplr(y_correct)))\r\n%%\r\nx=[10:-1:6 ; 1:5 ; 5:0.25:6];\r\ny_correct=[1:4 5:0.25:6 7:10];\r\nassert(isequal(assemble_this(x),y_correct)||isequal(assemble_this(x),fliplr(y_correct)))\r\n%%\r\nx=[8 16 24 ; 2 4 8 ; 6 4 2];\r\ny_correct=[6 4 2 4 8 16 24];\r\nassert(isequal(assemble_this(x),y_correct)||isequal(assemble_this(x),fliplr(y_correct)))\r\n%%\r\ny=ceil(rand*7)+5;\r\nry=[rand(1,y) y];\r\nfry=fliplr([y ry(1:end-1)]);\r\natf=assemble_this([fry ; ry]);\r\ny_correct=[y fry];\r\nassert(isequal(atf,y_correct)||isequal(atf,fliplr(y_correct)))\r\n%%\r\nt=rand(1,2);\r\nx=[8 16 24 ; 2 4 8 ; 6 4 2 ; 24 t];\r\nat=assemble_this(x);\r\ny_correct=[fliplr(t) 24 16 8 4 2 4 6];\r\nassert(isequal(at,y_correct)||isequal(at,fliplr(y_correct)))\r\n%%\r\nk=5+ceil(8*rand);\r\nx=randperm(k);\r\ny=randperm(k)+k;\r\nat=assemble_this([x x ; x y]);\r\ny_correct=[x x y];\r\nassert(isequal(at,y_correct)||isequal(at,fliplr(y_correct)))\r\n","published":true,"deleted":false,"likes_count":9,"comments_count":6,"created_by":1615,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":130,"test_suite_updated_at":"2018-01-08T18:23:27.000Z","rescore_all_solutions":true,"group_id":19,"created_at":"2012-06-21T18:36:35.000Z","updated_at":"2026-04-03T20:25:05.000Z","published_at":"2012-06-21T18:43:48.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 input to this function is a matrix of real numbers. Your job is to assemble the rows of the matrix into one large row that contains all of the individual rows of the matrix, and to make this output row as short as possible. You accomplish this by joining the rows together at the points at which they overlap. To help with the task, you can flip rows if you need to do so.\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\u003eFor example:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[input  = [1 1 2 3 ; 4 5 6 7 ; 5 4 8 3]\\n\\noutput = [1 1 2 3 8 4 5 6 7]]]\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:t\u003eExplanation: The [1 1 2 3] is the first row of the input.\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[3 8 4 5] is the third row (flipped) so the 3 at the end of the third row overlaps with the 3 at the end of the first row.\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[4 5 6 7] is the second row, with the [4 5] overlapping with the flipped [5 4] from the third row.\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\u003eOther than the mirrored version of the solution ([7 6 5 4 8 3 2 1 1] in the above example), each solution will be unique. Flipped versions of the entered value of y_correct will be tested for as well, in case your code comes up with that version of the correct answer.\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\u003eGood luck, and happy hunting.\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":"group:\"Matrix Manipulation I\"","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":"group:\"Matrix Manipulation I\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"group":[["group:\"Matrix Manipulation I\"","","\"","Matrix Manipulation I","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007f7362324d80\u003e":["Matrix Manipulation I"],"#\u003cMathWorks::Search::Field:0x00007f7362324ce0\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007f7362324420\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007f73623250a0\u003e":1,"#\u003cMathWorks::Search::Field:0x00007f7362325000\u003e":50,"#\u003cMathWorks::Search::Field:0x00007f7362324ec0\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007f7362324e20\u003e":"group:\"Matrix Manipulation I\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007f7362324e20\u003e":"group:\"Matrix Manipulation I\""},"queried_facets":{"#\u003cMathWorks::Search::Field:0x00007f7362324d80\u003e":["Matrix Manipulation I"]}},"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":"search","password":"J3bGPZzQ7asjJcCk","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":"group:\"Matrix Manipulation I\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"group":[["group:\"Matrix Manipulation I\"","","\"","Matrix Manipulation I","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007f7362324d80\u003e":["Matrix Manipulation I"],"#\u003cMathWorks::Search::Field:0x00007f7362324ce0\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007f7362324420\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007f73623250a0\u003e":1,"#\u003cMathWorks::Search::Field:0x00007f7362325000\u003e":50,"#\u003cMathWorks::Search::Field:0x00007f7362324ec0\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007f7362324e20\u003e":"group:\"Matrix Manipulation I\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007f7362324e20\u003e":"group:\"Matrix Manipulation I\""},"queried_facets":{"#\u003cMathWorks::Search::Field:0x00007f7362324d80\u003e":["Matrix Manipulation I"]}},"options":{"fields":["id","difficulty_rating"]},"join":" "},"results":[{"id":411,"difficulty_rating":"easy-medium"},{"id":413,"difficulty_rating":"easy-medium"},{"id":1129,"difficulty_rating":"easy-medium"},{"id":545,"difficulty_rating":"easy-medium"},{"id":794,"difficulty_rating":"easy-medium"},{"id":611,"difficulty_rating":"easy-medium"},{"id":497,"difficulty_rating":"easy-medium"},{"id":300,"difficulty_rating":"easy-medium"},{"id":798,"difficulty_rating":"easy-medium"},{"id":858,"difficulty_rating":"medium"},{"id":1042,"difficulty_rating":"medium"},{"id":792,"difficulty_rating":"medium"},{"id":796,"difficulty_rating":"medium"},{"id":112,"difficulty_rating":"medium"},{"id":375,"difficulty_rating":"medium-hard"},{"id":782,"difficulty_rating":"medium-hard"}]}}