{"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":46888,"title":"Diagonal Prod","description":null,"description_html":"\u003cdiv style = \"text-align: start; line-height: 20.44px; min-height: 0px; white-space: normal; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, monospace; font-style: normal; font-size: 14px; font-weight: 400; text-decoration: none solid rgb(0, 0, 0); white-space: normal; \"\u003e\u003cdiv style=\"block-size: 20.8px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 407px 10.4px; transform-origin: 407px 10.4px; vertical-align: baseline; \"\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.4px; text-align: left; transform-origin: 384px 10.4px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003ereturn element by element product of two diagonal vectors of two matrixes\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function y = your_fcn_name(x)\r\n  y = x;\r\nend","test_suite":"%%\r\nx = ones(3);\r\nz=zeros(3);\r\ny_correct = [0 0 0];\r\nassert(isequal(your_fcn_name(x,z),y_correct))\r\n\r\n%%\r\nx = ones(3);\r\nz=magic(3);\r\ny_correct = [8 5 2];\r\nassert(isequal(your_fcn_name(x,z),y_correct))\r\n","published":true,"deleted":false,"likes_count":0,"comments_count":0,"created_by":430136,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":90,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2020-10-17T15:43:56.000Z","updated_at":"2026-03-31T15:17:22.000Z","published_at":"2020-10-17T15:43:56.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ereturn element by element product of two diagonal vectors of two matrixes\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\",\"relationship\":null}],\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"target\":\"/matlab/document.xml\",\"relationshipId\":\"rId1\"}]}"},{"id":1158,"title":"Sum the elements in either diagonal of a square matrix","description":"Sum the elements of a square matrix that lie on either the major diagonal or anti-diagonal. (Include all elements that are part of either of the diagonals).","description_html":"\u003cp\u003eSum the elements of a square matrix that lie on either the major diagonal or anti-diagonal. (Include all elements that are part of either of the diagonals).\u003c/p\u003e","function_template":"function y = sumDiagonalElements(x)\r\n  y = 0;\r\nend","test_suite":"%%\r\nx = ones(2);\r\ny_correct = 4;\r\nassert(isequal(sumDiagonalElements(x),y_correct))\r\n%%\r\nx = ones(3);\r\ny_correct = 5;\r\nassert(isequal(sumDiagonalElements(x),y_correct))\r\n%%\r\nx = magic(8);\r\ny_correct = 520;\r\nassert(isequal(sumDiagonalElements(x),y_correct))\r\n%%\r\nx = magic(9);\r\ny_correct = 697;\r\nassert(isequal(sumDiagonalElements(x),y_correct))","published":true,"deleted":false,"likes_count":2,"comments_count":3,"created_by":8780,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":221,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2012-12-31T21:51:27.000Z","updated_at":"2026-02-09T20:24:17.000Z","published_at":"2012-12-31T21:53:05.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\u003eSum the elements of a square matrix that lie on either the major diagonal or anti-diagonal. (Include all elements that are part of either of the diagonals).\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":2365,"title":"Generate this matrix","description":"For a given odd integer n, generate a matrix as follows:\r\n\r\nInput: n = 5;\r\nOutput:\r\n  \r\n\r\n [  2     1     0     0     0\r\n    1     1     1     0     0\r\n    0     1     0     1     0\r\n    0     0     1     1     1\r\n    0     0     0     1     2  ]\r\n\r\n\r\nInput: n=7;\r\nOutput:\r\n   \r\n\r\n [  3     1     0     0     0     0     0\r\n    1     2     1     0     0     0     0\r\n    0     1     1     1     0     0     0\r\n    0     0     1     0     1     0     0\r\n    0     0     0     1     1     1     0\r\n    0     0     0     0     1     2     1\r\n    0     0     0     0     0     1     3 ]\r\n\r\nFor further understanding of the pattern, please check the test suite.","description_html":"\u003cp\u003eFor a given odd integer n, generate a matrix as follows:\u003c/p\u003e\u003cp\u003eInput: n = 5;\r\nOutput:\u003c/p\u003e\u003cpre\u003e [  2     1     0     0     0\r\n    1     1     1     0     0\r\n    0     1     0     1     0\r\n    0     0     1     1     1\r\n    0     0     0     1     2  ]\u003c/pre\u003e\u003cp\u003eInput: n=7;\r\nOutput:\u003c/p\u003e\u003cpre\u003e [  3     1     0     0     0     0     0\r\n    1     2     1     0     0     0     0\r\n    0     1     1     1     0     0     0\r\n    0     0     1     0     1     0     0\r\n    0     0     0     1     1     1     0\r\n    0     0     0     0     1     2     1\r\n    0     0     0     0     0     1     3 ]\u003c/pre\u003e\u003cp\u003eFor further understanding of the pattern, please check the test suite.\u003c/p\u003e","function_template":"function y = your_fcn_name(x)\r\n\r\nend","test_suite":"%%\r\nx = 7;\r\ny_correct = [3     1     0     0     0     0     0;\r\n     1     2     1     0     0     0     0;\r\n     0     1     1     1     0     0     0;\r\n     0     0     1     0     1     0     0;\r\n     0     0     0     1     1     1     0;\r\n     0     0     0     0     1     2     1;\r\n     0     0     0     0     0     1     3];\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n\r\n%%\r\nx = 5;\r\ny_correct =[2     1     0     0     0;\r\n     1     1     1     0     0;\r\n     0     1     0     1     0;\r\n     0     0     1     1     1;\r\n     0     0     0     1     2];\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n\r\n%%\r\nx = 11;\r\ny_correct =[5     1     0     0     0     0     0     0     0     0     0;\r\n     1     4     1     0     0     0     0     0     0     0     0;\r\n     0     1     3     1     0     0     0     0     0     0     0;\r\n     0     0     1     2     1     0     0     0     0     0     0;\r\n     0     0     0     1     1     1     0     0     0     0     0;\r\n     0     0     0     0     1     0     1     0     0     0     0;\r\n     0     0     0     0     0     1     1     1     0     0     0;\r\n     0     0     0     0     0     0     1     2     1     0     0;\r\n     0     0     0     0     0     0     0     1     3     1     0;\r\n     0     0     0     0     0     0     0     0     1     4     1;\r\n     0     0     0     0     0     0     0     0     0     1     5];\r\nassert(isequal(your_fcn_name(x),y_correct));\r\n\r\n%%\r\nx = 5;\r\ny_correct =[2     1     0     0     0;\r\n     1     1     1     0     0;\r\n     0     1     0     1     0;\r\n     0     0     1     1     1;\r\n     0     0     0     1     2];\r\nassert(isequal(your_fcn_name(x),y_correct));","published":true,"deleted":false,"likes_count":4,"comments_count":1,"created_by":17203,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":166,"test_suite_updated_at":"2014-06-17T20:20:00.000Z","rescore_all_solutions":false,"group_id":31,"created_at":"2014-06-17T20:16:19.000Z","updated_at":"2026-02-18T18:06:51.000Z","published_at":"2014-06-17T20:20:00.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\u003eFor a given odd integer n, generate a matrix as follows:\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: n = 5; Output:\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[ [  2     1     0     0     0\\n    1     1     1     0     0\\n    0     1     0     1     0\\n    0     0     1     1     1\\n    0     0     0     1     2  ]]]\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: n=7; Output:\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[ [  3     1     0     0     0     0     0\\n    1     2     1     0     0     0     0\\n    0     1     1     1     0     0     0\\n    0     0     1     0     1     0     0\\n    0     0     0     1     1     1     0\\n    0     0     0     0     1     2     1\\n    0     0     0     0     0     1     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\u003eFor further understanding of the pattern, please check the test suite.\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":859,"title":"Get the elements of diagonal and antidiagonal for any m-by-n matrix ","description":"In the problem \u003chttp://www.mathworks.com/matlabcentral/cody/problems/858-permute-diagonal-and-antidiagonal Problem 858. Permute diagonal and antidiagonal\u003e created by \u003chttp://www.mathworks.com/matlabcentral/cody/players/343082-jean-marie-sainthillier  Jean-Marie SAINTHILLIER\u003e the test suite is only based on squared m-by-m matrix .\r\n\r\nIn this problem you must submit a solution which *returns the diagonal and anti-diagonal elements without using the DIAG function*  for any *m-by-n matrix* \r\n\r\nExample:\r\n\r\nIf the input is a matrix 4*3\r\n\r\n  x =  [ 0     0     0\r\n         1    48    17\r\n         0     2    99\r\n         14    0    3]\r\n\r\nwe expect *2 output vectors*:\r\n\r\n  dg_elements = [0 48 99] % diagonal elements\r\n\r\n  antidg_elements = [14 2 17 ] % anti-diagonal elements\r\n\r\n\r\nNote : You do not have to permute anything. Just return the elements.","description_html":"\u003cp\u003eIn the problem \u003ca href=\"http://www.mathworks.com/matlabcentral/cody/problems/858-permute-diagonal-and-antidiagonal\"\u003eProblem 858. Permute diagonal and antidiagonal\u003c/a\u003e created by \u003ca href=\"http://www.mathworks.com/matlabcentral/cody/players/343082-jean-marie-sainthillier\"\u003eJean-Marie SAINTHILLIER\u003c/a\u003e the test suite is only based on squared m-by-m matrix .\u003c/p\u003e\u003cp\u003eIn this problem you must submit a solution which \u003cb\u003ereturns the diagonal and anti-diagonal elements without using the DIAG function\u003c/b\u003e  for any \u003cb\u003em-by-n matrix\u003c/b\u003e\u003c/p\u003e\u003cp\u003eExample:\u003c/p\u003e\u003cp\u003eIf the input is a matrix 4*3\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003ex =  [ 0     0     0\r\n       1    48    17\r\n       0     2    99\r\n       14    0    3]\r\n\u003c/pre\u003e\u003cp\u003ewe expect \u003cb\u003e2 output vectors\u003c/b\u003e:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003edg_elements = [0 48 99] % diagonal elements\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003eantidg_elements = [14 2 17 ] % anti-diagonal elements\r\n\u003c/pre\u003e\u003cp\u003eNote : You do not have to permute anything. Just return the elements.\u003c/p\u003e","function_template":"function [dg_elements,antidg_elements] = your_fcn_name(x)\r\n  dg_elements = 1 :8\r\n  antidg_elements = 8:-1:1\r\nend","test_suite":"%% no diag\r\nfiletext = fileread('your_fcn_name.m');\r\nassert(isempty(strfind(filetext, 'diag')))\r\n\r\n%%  square m-by-m matrix (1)\r\nx = [10 20 30; 40 50 60; 70 80 90];\r\ndiag_correct =  [10 50 90];\r\nantidiag_correct = [ 70    50    30];\r\n[A,B]= your_fcn_name(x);\r\nassert(isequal(A,diag_correct))\r\nassert(isequal(B,antidiag_correct))\r\n\r\n\r\n%% m-by-n matrix where m \u003e= n (2)\r\nx = [10 20 30; 40 50 60  ; 70 80 90; 100 74 15];\r\ndiag_corrct78 =  [10 50 90];\r\nantidiag_corrct78 = [ 100   80    60];\r\n[A3,B3]= your_fcn_name(x);\r\nassert(isequal(A3,diag_corrct78))\r\nassert(isequal(B3,antidiag_corrct78))\r\n\r\n\r\n%% m-by-n matrix where m \u003c= n (3)\r\nx = [10 20 30 58; 40 50 60 68 ; 70 80 90 98];\r\ndiag_correct7 =  [10 50 90];\r\nantidiag_correct7 = [ 70    50    30];\r\n[A3,B3]= your_fcn_name(x);\r\nassert(isequal(A3,diag_correct7))\r\nassert(isequal(B3,antidiag_correct7))\r\n\r\n%% m-by-n matrix where m \u003e= n (4)\r\nt = [0 0 0; 1 0 0 ; 0  2 0; 0 0 3];\r\ndiag8 =  [0 0 0];\r\nantid7 = [ 0 2 0];\r\n[A3,B3]= your_fcn_name(t);\r\nassert(isequal(A3,diag8))\r\nassert(isequal(B3,antid7))\r\n\r\n\r\n% m-by-n matrix where m \u003c= n (5)\r\nt = [0 1 0 0; 0 0 2 0 ; 0 0 0 3];\r\ndiag8 =  [0 0 0];\r\nantid7 = [ 0 0 0];\r\n[A3,B3]= your_fcn_name(t);\r\nassert(isequal(A3,diag8))\r\nassert(isequal(B3,antid7))","published":true,"deleted":false,"likes_count":11,"comments_count":2,"created_by":639,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":513,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":12,"created_at":"2012-07-24T07:26:53.000Z","updated_at":"2026-02-10T12:04:49.000Z","published_at":"2012-07-24T07:26:53.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\u003eIn the problem\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://www.mathworks.com/matlabcentral/cody/problems/858-permute-diagonal-and-antidiagonal\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eProblem 858. Permute diagonal and antidiagonal\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e created by\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://www.mathworks.com/matlabcentral/cody/players/343082-jean-marie-sainthillier\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eJean-Marie SAINTHILLIER\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e the test suite is only based on squared m-by-m matrix .\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 this problem you must submit a solution which\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\u003ereturns the diagonal and anti-diagonal elements without using the DIAG function\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e for any\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\u003em-by-n matrix\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=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eIf the input is a matrix 4*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[x =  [ 0     0     0\\n       1    48    17\\n       0     2    99\\n       14    0    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\u003ewe expect\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\u003e2 output vectors\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[dg_elements = [0 48 99] % diagonal elements\\n\\nantidg_elements = [14 2 17 ] % anti-diagonal elements]]\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 : You do not have to permute anything. Just return the elements.\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":2587,"title":"Find indices of diagonal elements","description":"Given a matrix A  and a diagonal index d (where 0 indicates the main diagonal and off-diagonals are identified by positive and negative integers), return the indices of the elements of this diagonal. \r\n\r\nd is always correct but A can contain repetitions, so ...\r\n","description_html":"\u003cp\u003eGiven a matrix A  and a diagonal index d (where 0 indicates the main diagonal and off-diagonals are identified by positive and negative integers), return the indices of the elements of this diagonal.\u003c/p\u003e\u003cp\u003ed is always correct but A can contain repetitions, so ...\u003c/p\u003e","function_template":"function y = diag_indices(A,d)\r\n  y = d;\r\nend","test_suite":"%%\r\nx = [1 2 3;4 5 6;7 8 9];\r\na = [1 5 9];\r\nassert(isequal(diag_indices(x,0),a))\r\n%%\r\nx = magic(5);\r\nb = 5;\r\nassert(isequal(diag_indices(x,-4),b))\r\n%%\r\nx = magic(5);\r\na = 21;\r\nassert(isequal(diag_indices(x,4),a))\r\n%%\r\nx = magic(5);\r\nz = 5;\r\nassert(isequal(diag_indices(x,-4),z))\r\n%%\r\nx = magic(6);\r\na = [19 26 33];\r\nassert(isequal(diag_indices(x,3),a))\r\n%%\r\nx1 = magic(50);\r\ny_correct = 50;\r\nassert(isequal(diag_indices(x1,-49),y_correct))\r\n%%\r\nx2 = magic(50);\r\ny_correct = [49 100];\r\nassert(isequal(diag_indices(x2,-48),y_correct))\r\n%%\r\ny = magic(50);\r\nv = [48 99 150];\r\nassert(isequal(diag_indices(y,-47),v))\r\n%%\r\nv=[1 2 3;4 5 6;1 2 3];\r\nt = [1 5 9];\r\nassert(isequal(diag_indices(v,0),t))\r\n%%\r\nx=[1 2;1 2];\r\ny_correct = 3;\r\nassert(isequal(diag_indices(x,1),y_correct))\r\n%%\r\nx=[1 1 1;1 1 1;1 1 1];\r\nt =  [1 5 9];\r\nassert(isequal(diag_indices(x,0),t))\r\n%%\r\ny=[1 2 3;1 2 3];\r\ny_correct = [1 4];\r\nassert(isequal(diag_indices(y,0),y_correct))\r\n","published":true,"deleted":false,"likes_count":2,"comments_count":2,"created_by":5390,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":73,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":39,"created_at":"2014-09-15T11:37:21.000Z","updated_at":"2026-04-02T08:13:56.000Z","published_at":"2014-09-15T11:38: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\",\"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\u003eGiven a matrix A and a diagonal index d (where 0 indicates the main diagonal and off-diagonals are identified by positive and negative integers), return the indices of the elements of this 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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ed is always correct but A can contain repetitions, so ...\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":45578,"title":"Create a matrix that counts up diagonally","description":"Given a single input _N_, create a _N_ x _N_ matrix that counts from 1 : _N_ ², (along up-right diagonals, starting with 1 in the top left corner. For example, given N=4...\r\n\r\n  1  3  6 10\r\n  2  5  9 13 \r\n  4  8 12 15 \r\n  7 11 14 16\r\n  \r\n\r\nNotice as you move up a row and right a column (↗) the values always increase by one. The value '1' should always go in the top left corner, with '2' directly below it. From there fill in the upward-rightward diagonals with the next higher integer until the matrix is complete. Assume N will always be a positive integer greater than 1 (N \u003e= 2).\r\n","description_html":"\u003cp\u003eGiven a single input \u003ci\u003eN\u003c/i\u003e, create a \u003ci\u003eN\u003c/i\u003e x \u003ci\u003eN\u003c/i\u003e matrix that counts from 1 : \u003ci\u003eN\u003c/i\u003e ², (along up-right diagonals, starting with 1 in the top left corner. For example, given N=4...\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e1  3  6 10\r\n2  5  9 13 \r\n4  8 12 15 \r\n7 11 14 16\r\n\u003c/pre\u003e\u003cp\u003eNotice as you move up a row and right a column (↗) the values always increase by one. The value '1' should always go in the top left corner, with '2' directly below it. From there fill in the upward-rightward diagonals with the next higher integer until the matrix is complete. Assume N will always be a positive integer greater than 1 (N \u0026gt;= 2).\u003c/p\u003e","function_template":"function mx = not_hankel(N)\r\n\r\n    mx = ones(N); %replace with your code\r\n\r\nend","test_suite":"%%\r\nN = 20;\r\n\r\nmx_correct = [...\r\n     1     3     6    10    15    21    28    36    45    55    66    78    91   105   120   136   153   171   190   210\r\n     2     5     9    14    20    27    35    44    54    65    77    90   104   119   135   152   170   189   209   229\r\n     4     8    13    19    26    34    43    53    64    76    89   103   118   134   151   169   188   208   228   247\r\n     7    12    18    25    33    42    52    63    75    88   102   117   133   150   168   187   207   227   246   264\r\n    11    17    24    32    41    51    62    74    87   101   116   132   149   167   186   206   226   245   263   280\r\n    16    23    31    40    50    61    73    86   100   115   131   148   166   185   205   225   244   262   279   295\r\n    22    30    39    49    60    72    85    99   114   130   147   165   184   204   224   243   261   278   294   309\r\n    29    38    48    59    71    84    98   113   129   146   164   183   203   223   242   260   277   293   308   322\r\n    37    47    58    70    83    97   112   128   145   163   182   202   222   241   259   276   292   307   321   334\r\n    46    57    69    82    96   111   127   144   162   181   201   221   240   258   275   291   306   320   333   345\r\n    56    68    81    95   110   126   143   161   180   200   220   239   257   274   290   305   319   332   344   355\r\n    67    80    94   109   125   142   160   179   199   219   238   256   273   289   304   318   331   343   354   364\r\n    79    93   108   124   141   159   178   198   218   237   255   272   288   303   317   330   342   353   363   372\r\n    92   107   123   140   158   177   197   217   236   254   271   287   302   316   329   341   352   362   371   379\r\n   106   122   139   157   176   196   216   235   253   270   286   301   315   328   340   351   361   370   378   385\r\n   121   138   156   175   195   215   234   252   269   285   300   314   327   339   350   360   369   377   384   390\r\n   137   155   174   194   214   233   251   268   284   299   313   326   338   349   359   368   376   383   389   394\r\n   154   173   193   213   232   250   267   283   298   312   325   337   348   358   367   375   382   388   393   397\r\n   172   192   212   231   249   266   282   297   311   324   336   347   357   366   374   381   387   392   396   399\r\n   191   211   230   248   265   281   296   310   323   335   346   356   365   373   380   386   391   395   398   400\r\n   ];\r\n\r\nassert(isequal(not_hankel(N),mx_correct))\r\n\r\n\r\n\r\n%%\r\nN = 3;\r\n\r\nmx_correct = [...\r\n     1     3     6\r\n     2     5     8\r\n     4     7     9\r\n     ];\r\n\r\nassert(isequal(not_hankel(N),mx_correct))\r\n\r\n\r\n\r\n%%\r\nrng('shuffle')\r\nN  = randi(99)+5;\r\nr = repmat((0:(N-1))',1,N) + (0:(N-1));\r\np = ((.5.*r.^2 + .5.*r) + (r(1,:)+1));\r\nq = rot90(hankel(fliplr(0:N-1)),2).^2;\r\nmx_correct = p - q;\r\nassert(isequal(not_hankel(N),mx_correct))\r\n\r\n\r\n\r\n%% \r\nN = 2;\r\nmx_correct =...\r\n[...\r\n    1  3\r\n    2  4 \r\n];\r\n\r\nassert(isequal(not_hankel(N),mx_correct))","published":true,"deleted":false,"likes_count":2,"comments_count":0,"created_by":18354,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":22,"test_suite_updated_at":"2020-05-22T17:05:13.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2020-05-22T16:46:56.000Z","updated_at":"2026-01-20T13:24:26.000Z","published_at":"2020-05-22T16:46:56.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 single input\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\u003eN\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, create a\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\u003eN\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e x\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\u003eN\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e matrix that counts from 1 :\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\u003eN\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e ², (along up-right diagonals, starting with 1 in the top left corner. For example, given N=4...\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  3  6 10\\n2  5  9 13 \\n4  8 12 15 \\n7 11 14 16]]\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\u003eNotice as you move up a row and right a column (↗) the values always increase by one. The value '1' should always go in the top left corner, with '2' directly below it. From there fill in the upward-rightward diagonals with the next higher integer until the matrix is complete. Assume N will always be a positive integer greater than 1 (N \u0026gt;= 2).\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":2455,"title":"Diagonal of a Spiral Matrix","description":"Write a function that will return same output as diag(spiral(n)). The only exception is that spiral and diag functions are not allowed in your function. Also, loops are not allowed.","description_html":"\u003cp\u003eWrite a function that will return same output as diag(spiral(n)). The only exception is that spiral and diag functions are not allowed in your function. Also, loops are not allowed.\u003c/p\u003e","function_template":"function y=digSpi(n)\r\n\r\n\r\n\r\n\r\nend","test_suite":"s = evalc('type digSpi');\r\n\r\nif ~isempty(regexp(s,'error'))\r\n    error('error using error');\r\nend\r\n\r\nif ~isempty(regexp(s,'assert'))\r\n    error('error using assert');\r\nend\r\n\r\nif ~isempty(regexp(s,'diag'))\r\n    error('error using diag');\r\nend\r\n\r\nif ~isempty(regexp(s,'spiral'))\r\n    error('error using spiral');\r\nend\r\n\r\nif ~isempty(regexp(s,'for'))\r\n    error('error using for');\r\nend\r\n\r\nif ~isempty(regexp(s,'while'))\r\n    error('error using while');\r\nend\r\n\r\n%%\r\n\r\na = diag(spiral(4));\r\nb = digSpi(4);\r\nassert(isequal(a,b));\r\n\r\n%%\r\n\r\na = diag(spiral(5));\r\nb = digSpi(5);\r\nassert(isequal(a,b));\r\n\r\n%%\r\n\r\na = diag(spiral(6));\r\nb = digSpi(6);\r\nassert(isequal(a,b));\r\n\r\n%%\r\n\r\na = diag(spiral(7));\r\nb = digSpi(7);\r\nassert(isequal(a,b));\r\n\r\n%%\r\n\r\na = diag(spiral(8));\r\nb = digSpi(8);\r\nassert(isequal(a,b));","published":true,"deleted":false,"likes_count":2,"comments_count":0,"created_by":17203,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":111,"test_suite_updated_at":"2014-07-21T18:30:07.000Z","rescore_all_solutions":false,"group_id":31,"created_at":"2014-07-20T08:15:09.000Z","updated_at":"2026-03-04T17:27:33.000Z","published_at":"2014-07-20T08:15:09.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\u003eWrite a function that will return same output as diag(spiral(n)). The only exception is that spiral and diag functions are not allowed in your function. Also, loops are not allowed.\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":59242,"title":"Construct numbers from antidiagonals of stacks of binary numbers","description":"A sequence is constructed in the following way: Express the numbers 1, 2, 3, 4, 5,… in binary and stack them, right-justified. Then read the numbers in upward sloping diagonals and convert to decimal. The first four binary numbers are 11, 110, 101, 100, and their decimal equivalents are 3, 6, 5, 4. \r\n0 0 0 1\r\n0 0 1 0 \r\n0 0 1 1 \r\n0 1 0 0\r\n0 1 0 1\r\n0 1 1 0 \r\nWrite a function to compute the th term in the sequence. ","description_html":"\u003cdiv style = \"text-align: start; line-height: 20.4333px; min-height: 0px; white-space: normal; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, monospace; font-style: normal; font-size: 14px; font-weight: 400; text-decoration: rgb(0, 0, 0); white-space: normal; \"\u003e\u003cdiv style=\"block-size: 273px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 407px 136.5px; transform-origin: 407px 136.5px; vertical-align: baseline; \"\u003e\u003cdiv style=\"block-size: 63px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 31.5px; text-align: left; transform-origin: 384px 31.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 384px 8px; transform-origin: 384px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eA sequence is constructed in the following way: Express the numbers 1, 2, 3, 4, 5,… in binary and stack them, right-justified. Then read the numbers in upward sloping diagonals and convert to decimal. The first four binary numbers are 11, 110, 101, 100, and their decimal equivalents are 3, 6, 5, 4. \u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 26.95px 8px; transform-origin: 26.95px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"font-family: Menlo, Monaco, Consolas, \u0026quot;Courier New\u0026quot;, monospace; \"\u003e0 0 0 1\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 30.8px 8px; transform-origin: 30.8px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"font-family: Menlo, Monaco, Consolas, \u0026quot;Courier New\u0026quot;, monospace; \"\u003e0 0 1 0 \u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 30.8px 8px; transform-origin: 30.8px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"font-family: Menlo, Monaco, Consolas, \u0026quot;Courier New\u0026quot;, monospace; \"\u003e0 0 1 1 \u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 26.95px 8px; transform-origin: 26.95px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"font-family: Menlo, Monaco, Consolas, \u0026quot;Courier New\u0026quot;, monospace; \"\u003e0 1 0 0\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 26.95px 8px; transform-origin: 26.95px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"font-family: Menlo, Monaco, Consolas, \u0026quot;Courier New\u0026quot;, monospace; \"\u003e0 1 0 1\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 30.8px 8px; transform-origin: 30.8px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"font-family: Menlo, Monaco, Consolas, \u0026quot;Courier New\u0026quot;, monospace; \"\u003e0 1 1 0 \u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 98.6583px 8px; transform-origin: 98.6583px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eWrite a function to compute the \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"font-family: \u0026quot;STIXGeneral\u0026quot;, \u0026quot;STIXGeneral-webfont\u0026quot;, serif; font-style: italic; font-weight: 400; color: rgb(0, 0, 0);\"\u003en\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 77.0083px 8px; transform-origin: 77.0083px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eth term in the sequence. \u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function y = slopingBinary(n)\r\n  y = diag(dec2bin(1:n)');\r\nend","test_suite":"%%\r\nn = 4;\r\ny_correct = 4;\r\nassert(isequal(slopingBinary(n),y_correct))\r\n\r\n%%\r\nn = 12;\r\ny_correct = 28;\r\nassert(isequal(slopingBinary(n),y_correct))\r\n\r\n%%\r\nn = 27;\r\ny_correct = 61;\r\nassert(isequal(slopingBinary(n),y_correct))\r\n\r\n%%\r\nn = 59;\r\ny_correct = 93;\r\nassert(isequal(slopingBinary(n),y_correct))\r\n\r\n%%\r\nn = 122;\r\ny_correct = 190;\r\nassert(isequal(slopingBinary(n),y_correct))\r\n\r\n%%\r\nn = 249;\r\ny_correct = 379;\r\nassert(isequal(slopingBinary(n),y_correct))\r\n\r\n%%\r\nn = 1015;\r\ny_correct = 1529;\r\nassert(isequal(slopingBinary(n),y_correct))\r\n\r\n%%\r\nn = 2038;\r\ny_correct = 3066;\r\nassert(isequal(slopingBinary(n),y_correct))\r\n\r\n%%\r\nn = 4085;\r\ny_correct = 6143;\r\nassert(isequal(slopingBinary(n),y_correct))\r\n\r\n%%\r\nn = 8180;\r\ny_correct = 12276;\r\nassert(isequal(slopingBinary(n),y_correct))\r\n\r\n%%\r\nn = 16537;\r\ny_correct = 16539;\r\nassert(isequal(slopingBinary(n),y_correct))\r\n\r\n%%\r\nn = 485373;\r\ny_correct = 485383;\r\nassert(isequal(slopingBinary(n),y_correct))\r\n\r\n%%\r\nn = 7341901;\r\ny_correct = 7341911;\r\nassert(isequal(slopingBinary(n),y_correct))\r\n\r\n%%\r\nn = 55988359;\r\ny_correct = 55988361;\r\nassert(isequal(slopingBinary(n),y_correct))\r\n\r\n%%\r\nn = 1073741824;\r\nassert(isequal(slopingBinary(n),n))\r\n\r\n%%\r\nn = 17179869184;\r\nassert(isequal(slopingBinary(n),n))\r\n\r\n%%\r\nassert(isequal(slopingBinary(flintmax/2),flintmax/2))\r\n\r\n%%\r\nn = 899423417;\r\nyyyy_correct = 899423465;\r\ny = slopingBinary(slopingBinary(slopingBinary(slopingBinary(n))));\r\nassert(isequal(y,yyyy_correct))\r\n\r\n%%\r\nfiletext = fileread('slopingBinary.m');\r\nillegal = contains(filetext, 'assignin') || contains(filetext, 'assert') || contains(filetext,'regexp'); \r\nassert(~illegal)","published":true,"deleted":false,"likes_count":1,"comments_count":2,"created_by":46909,"edited_by":46909,"edited_at":"2023-11-28T03:00:34.000Z","deleted_by":null,"deleted_at":null,"solvers_count":5,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2023-11-28T03:00:26.000Z","updated_at":"2023-11-28T03:00:34.000Z","published_at":"2023-11-28T03:00:34.000Z","restored_at":null,"restored_by":null,"spam":null,"simulink":false,"admin_reviewed":false,"description_opc":"{\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eA sequence is constructed in the following way: Express the numbers 1, 2, 3, 4, 5,… in binary and stack them, right-justified. Then read the numbers in upward sloping diagonals and convert to decimal. The first four binary numbers are 11, 110, 101, 100, and their decimal equivalents are 3, 6, 5, 4. \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:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e0 0 0 1\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e0 0 1 0 \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:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e0 0 1 1 \u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e0 1 0 0\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:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e0 1 0 1\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e0 1 1 0 \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\u003eWrite a function to compute the \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:customXml w:element=\\\"equation\\\"\u003e\u003cw:customXmlPr\u003e\u003cw:attr w:name=\\\"displayStyle\\\" w:val=\\\"false\\\"/\u003e\u003cw:attr w:name=\\\"altTextString\\\" w:val=\\\"n\\\"/\u003e\u003c/w:customXmlPr\u003e\u003cw:r\u003e\u003cw:t\u003en\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:customXml\u003e\u003cw:r\u003e\u003cw:t\u003eth term in the sequence. \u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\",\"relationship\":null}],\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"target\":\"/matlab/document.xml\",\"relationshipId\":\"rId1\"}]}"},{"id":2585,"title":"How to reverse spdiags?","description":"The spdiags function extracts all nonzero diagonals from the m-by-n matrix A.\r\n\r\nSo if A=[1 2 3;4 5 6;7 8 9],\r\n\r\nspdiags(A)=[7 4 1 0 0;0 8 5 2 0;0 0 9 6 3].\r\n\r\nBut if I give you spdiags(A), can you give me A ?","description_html":"\u003cdiv style = \"text-align: start; line-height: 20.44px; min-height: 0px; white-space: normal; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, monospace; font-style: normal; font-size: 14px; font-weight: 400; text-decoration: none solid rgb(0, 0, 0); white-space: normal; \"\u003e\u003cdiv style=\"block-size: 213px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 332px 106.5px; transform-origin: 332px 106.5px; vertical-align: baseline; \"\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 309px 10.5px; text-align: left; transform-origin: 309px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eThe spdiags function extracts all nonzero diagonals from the m-by-n matrix A.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 309px 10.5px; text-align: left; transform-origin: 309px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eSo if\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgb(247, 247, 247); block-size: 20px; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; margin-block-end: 10px; margin-block-start: 10px; margin-bottom: 10px; margin-inline-end: 3px; margin-inline-start: 3px; margin-left: 3px; margin-right: 3px; margin-top: 10px; perspective-origin: 329px 10px; transform-origin: 329px 10px; margin-left: 3px; margin-top: 10px; margin-bottom: 10px; margin-right: 3px; \"\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 0px 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e  A = [1 2 3;4 5 6;7 8 9]\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 10px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 10px; perspective-origin: 309px 10.5px; text-align: left; transform-origin: 309px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 10px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003ethen\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgb(247, 247, 247); block-size: 60px; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; margin-block-end: 10px; margin-block-start: 10px; margin-bottom: 10px; margin-inline-end: 3px; margin-inline-start: 3px; margin-left: 3px; margin-right: 3px; margin-top: 10px; perspective-origin: 329px 30px; transform-origin: 329px 30px; margin-left: 3px; margin-top: 10px; margin-bottom: 10px; margin-right: 3px; \"\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 329px 10px; transform-origin: 329px 10px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 0px 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e  spdiags(A) = [7 4 1 0 0;\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 329px 10px; transform-origin: 329px 10px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 0px 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e                0 8 5 2 0;\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 329px 10px; transform-origin: 329px 10px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 0px 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e                0 0 9 6 3]\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 10px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 10px; perspective-origin: 309px 10.5px; text-align: left; transform-origin: 309px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 10px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eBut if I give you spdiags(A), can you give me A ?\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function y = inv_spdiags(x)\r\n  y = x;\r\nend","test_suite":"%%\r\nx = spdiags([1 2 3;4 5 6;7 8 9]);\r\nassert(isequal(spdiags(inv_spdiags(x)),x))\r\n%%\r\nb = spdiags(eye(10));\r\nassert(isequal(spdiags(inv_spdiags(b)),b))\r\n%%\r\nx = spdiags([1 2 3;4 5 6]);\r\nassert(isequal(spdiags(inv_spdiags(x)),x))\r\n%%\r\ny = spdiags(magic(9));\r\nassert(isequal(spdiags(inv_spdiags(y)),y))\r\n%%\r\nx = spdiags(magic(5));\r\nassert(isequal(spdiags(inv_spdiags(x)),x))\r\n%%\r\nz = spdiags(ones(10));\r\nassert(isequal(spdiags(inv_spdiags(z)),z))\r\n%%\r\nx = spdiags(randi(10,5));\r\nassert(isequal(spdiags(inv_spdiags(x)),x))\r\n%%\r\nx = spdiags(randi(5,[3 2]));\r\nassert(isequal(spdiags(inv_spdiags(x)),x))\r\n%%\r\na = spdiags(zeros(5));\r\nassert(isequal(spdiags(inv_spdiags(a)),a))","published":true,"deleted":false,"likes_count":2,"comments_count":1,"created_by":5390,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":43,"test_suite_updated_at":"2014-09-14T14:43:49.000Z","rescore_all_solutions":false,"group_id":39,"created_at":"2014-09-14T14:39:28.000Z","updated_at":"2026-02-15T11:51:56.000Z","published_at":"2014-09-14T14:41:28.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe spdiags function extracts all nonzero diagonals from the m-by-n matrix 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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eSo if\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;4 5 6;7 8 9]]]\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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ethen\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[  spdiags(A) = [7 4 1 0 0;\\n                0 8 5 2 0;\\n                0 0 9 6 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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eBut if I give you spdiags(A), can you give me A ?\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\",\"relationship\":null}],\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"target\":\"/matlab/document.xml\",\"relationshipId\":\"rId1\"}]}"}],"problem_search":{"errors":[],"problems":[{"id":46888,"title":"Diagonal Prod","description":null,"description_html":"\u003cdiv style = \"text-align: start; line-height: 20.44px; min-height: 0px; white-space: normal; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, monospace; font-style: normal; font-size: 14px; font-weight: 400; text-decoration: none solid rgb(0, 0, 0); white-space: normal; \"\u003e\u003cdiv style=\"block-size: 20.8px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 407px 10.4px; transform-origin: 407px 10.4px; vertical-align: baseline; \"\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.4px; text-align: left; transform-origin: 384px 10.4px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003ereturn element by element product of two diagonal vectors of two matrixes\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function y = your_fcn_name(x)\r\n  y = x;\r\nend","test_suite":"%%\r\nx = ones(3);\r\nz=zeros(3);\r\ny_correct = [0 0 0];\r\nassert(isequal(your_fcn_name(x,z),y_correct))\r\n\r\n%%\r\nx = ones(3);\r\nz=magic(3);\r\ny_correct = [8 5 2];\r\nassert(isequal(your_fcn_name(x,z),y_correct))\r\n","published":true,"deleted":false,"likes_count":0,"comments_count":0,"created_by":430136,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":90,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2020-10-17T15:43:56.000Z","updated_at":"2026-03-31T15:17:22.000Z","published_at":"2020-10-17T15:43:56.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ereturn element by element product of two diagonal vectors of two matrixes\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\",\"relationship\":null}],\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"target\":\"/matlab/document.xml\",\"relationshipId\":\"rId1\"}]}"},{"id":1158,"title":"Sum the elements in either diagonal of a square matrix","description":"Sum the elements of a square matrix that lie on either the major diagonal or anti-diagonal. (Include all elements that are part of either of the diagonals).","description_html":"\u003cp\u003eSum the elements of a square matrix that lie on either the major diagonal or anti-diagonal. (Include all elements that are part of either of the diagonals).\u003c/p\u003e","function_template":"function y = sumDiagonalElements(x)\r\n  y = 0;\r\nend","test_suite":"%%\r\nx = ones(2);\r\ny_correct = 4;\r\nassert(isequal(sumDiagonalElements(x),y_correct))\r\n%%\r\nx = ones(3);\r\ny_correct = 5;\r\nassert(isequal(sumDiagonalElements(x),y_correct))\r\n%%\r\nx = magic(8);\r\ny_correct = 520;\r\nassert(isequal(sumDiagonalElements(x),y_correct))\r\n%%\r\nx = magic(9);\r\ny_correct = 697;\r\nassert(isequal(sumDiagonalElements(x),y_correct))","published":true,"deleted":false,"likes_count":2,"comments_count":3,"created_by":8780,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":221,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2012-12-31T21:51:27.000Z","updated_at":"2026-02-09T20:24:17.000Z","published_at":"2012-12-31T21:53:05.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\u003eSum the elements of a square matrix that lie on either the major diagonal or anti-diagonal. (Include all elements that are part of either of the diagonals).\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":2365,"title":"Generate this matrix","description":"For a given odd integer n, generate a matrix as follows:\r\n\r\nInput: n = 5;\r\nOutput:\r\n  \r\n\r\n [  2     1     0     0     0\r\n    1     1     1     0     0\r\n    0     1     0     1     0\r\n    0     0     1     1     1\r\n    0     0     0     1     2  ]\r\n\r\n\r\nInput: n=7;\r\nOutput:\r\n   \r\n\r\n [  3     1     0     0     0     0     0\r\n    1     2     1     0     0     0     0\r\n    0     1     1     1     0     0     0\r\n    0     0     1     0     1     0     0\r\n    0     0     0     1     1     1     0\r\n    0     0     0     0     1     2     1\r\n    0     0     0     0     0     1     3 ]\r\n\r\nFor further understanding of the pattern, please check the test suite.","description_html":"\u003cp\u003eFor a given odd integer n, generate a matrix as follows:\u003c/p\u003e\u003cp\u003eInput: n = 5;\r\nOutput:\u003c/p\u003e\u003cpre\u003e [  2     1     0     0     0\r\n    1     1     1     0     0\r\n    0     1     0     1     0\r\n    0     0     1     1     1\r\n    0     0     0     1     2  ]\u003c/pre\u003e\u003cp\u003eInput: n=7;\r\nOutput:\u003c/p\u003e\u003cpre\u003e [  3     1     0     0     0     0     0\r\n    1     2     1     0     0     0     0\r\n    0     1     1     1     0     0     0\r\n    0     0     1     0     1     0     0\r\n    0     0     0     1     1     1     0\r\n    0     0     0     0     1     2     1\r\n    0     0     0     0     0     1     3 ]\u003c/pre\u003e\u003cp\u003eFor further understanding of the pattern, please check the test suite.\u003c/p\u003e","function_template":"function y = your_fcn_name(x)\r\n\r\nend","test_suite":"%%\r\nx = 7;\r\ny_correct = [3     1     0     0     0     0     0;\r\n     1     2     1     0     0     0     0;\r\n     0     1     1     1     0     0     0;\r\n     0     0     1     0     1     0     0;\r\n     0     0     0     1     1     1     0;\r\n     0     0     0     0     1     2     1;\r\n     0     0     0     0     0     1     3];\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n\r\n%%\r\nx = 5;\r\ny_correct =[2     1     0     0     0;\r\n     1     1     1     0     0;\r\n     0     1     0     1     0;\r\n     0     0     1     1     1;\r\n     0     0     0     1     2];\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n\r\n%%\r\nx = 11;\r\ny_correct =[5     1     0     0     0     0     0     0     0     0     0;\r\n     1     4     1     0     0     0     0     0     0     0     0;\r\n     0     1     3     1     0     0     0     0     0     0     0;\r\n     0     0     1     2     1     0     0     0     0     0     0;\r\n     0     0     0     1     1     1     0     0     0     0     0;\r\n     0     0     0     0     1     0     1     0     0     0     0;\r\n     0     0     0     0     0     1     1     1     0     0     0;\r\n     0     0     0     0     0     0     1     2     1     0     0;\r\n     0     0     0     0     0     0     0     1     3     1     0;\r\n     0     0     0     0     0     0     0     0     1     4     1;\r\n     0     0     0     0     0     0     0     0     0     1     5];\r\nassert(isequal(your_fcn_name(x),y_correct));\r\n\r\n%%\r\nx = 5;\r\ny_correct =[2     1     0     0     0;\r\n     1     1     1     0     0;\r\n     0     1     0     1     0;\r\n     0     0     1     1     1;\r\n     0     0     0     1     2];\r\nassert(isequal(your_fcn_name(x),y_correct));","published":true,"deleted":false,"likes_count":4,"comments_count":1,"created_by":17203,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":166,"test_suite_updated_at":"2014-06-17T20:20:00.000Z","rescore_all_solutions":false,"group_id":31,"created_at":"2014-06-17T20:16:19.000Z","updated_at":"2026-02-18T18:06:51.000Z","published_at":"2014-06-17T20:20:00.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\u003eFor a given odd integer n, generate a matrix as follows:\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: n = 5; Output:\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[ [  2     1     0     0     0\\n    1     1     1     0     0\\n    0     1     0     1     0\\n    0     0     1     1     1\\n    0     0     0     1     2  ]]]\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: n=7; Output:\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[ [  3     1     0     0     0     0     0\\n    1     2     1     0     0     0     0\\n    0     1     1     1     0     0     0\\n    0     0     1     0     1     0     0\\n    0     0     0     1     1     1     0\\n    0     0     0     0     1     2     1\\n    0     0     0     0     0     1     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\u003eFor further understanding of the pattern, please check the test suite.\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":859,"title":"Get the elements of diagonal and antidiagonal for any m-by-n matrix ","description":"In the problem \u003chttp://www.mathworks.com/matlabcentral/cody/problems/858-permute-diagonal-and-antidiagonal Problem 858. Permute diagonal and antidiagonal\u003e created by \u003chttp://www.mathworks.com/matlabcentral/cody/players/343082-jean-marie-sainthillier  Jean-Marie SAINTHILLIER\u003e the test suite is only based on squared m-by-m matrix .\r\n\r\nIn this problem you must submit a solution which *returns the diagonal and anti-diagonal elements without using the DIAG function*  for any *m-by-n matrix* \r\n\r\nExample:\r\n\r\nIf the input is a matrix 4*3\r\n\r\n  x =  [ 0     0     0\r\n         1    48    17\r\n         0     2    99\r\n         14    0    3]\r\n\r\nwe expect *2 output vectors*:\r\n\r\n  dg_elements = [0 48 99] % diagonal elements\r\n\r\n  antidg_elements = [14 2 17 ] % anti-diagonal elements\r\n\r\n\r\nNote : You do not have to permute anything. Just return the elements.","description_html":"\u003cp\u003eIn the problem \u003ca href=\"http://www.mathworks.com/matlabcentral/cody/problems/858-permute-diagonal-and-antidiagonal\"\u003eProblem 858. Permute diagonal and antidiagonal\u003c/a\u003e created by \u003ca href=\"http://www.mathworks.com/matlabcentral/cody/players/343082-jean-marie-sainthillier\"\u003eJean-Marie SAINTHILLIER\u003c/a\u003e the test suite is only based on squared m-by-m matrix .\u003c/p\u003e\u003cp\u003eIn this problem you must submit a solution which \u003cb\u003ereturns the diagonal and anti-diagonal elements without using the DIAG function\u003c/b\u003e  for any \u003cb\u003em-by-n matrix\u003c/b\u003e\u003c/p\u003e\u003cp\u003eExample:\u003c/p\u003e\u003cp\u003eIf the input is a matrix 4*3\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003ex =  [ 0     0     0\r\n       1    48    17\r\n       0     2    99\r\n       14    0    3]\r\n\u003c/pre\u003e\u003cp\u003ewe expect \u003cb\u003e2 output vectors\u003c/b\u003e:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003edg_elements = [0 48 99] % diagonal elements\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003eantidg_elements = [14 2 17 ] % anti-diagonal elements\r\n\u003c/pre\u003e\u003cp\u003eNote : You do not have to permute anything. Just return the elements.\u003c/p\u003e","function_template":"function [dg_elements,antidg_elements] = your_fcn_name(x)\r\n  dg_elements = 1 :8\r\n  antidg_elements = 8:-1:1\r\nend","test_suite":"%% no diag\r\nfiletext = fileread('your_fcn_name.m');\r\nassert(isempty(strfind(filetext, 'diag')))\r\n\r\n%%  square m-by-m matrix (1)\r\nx = [10 20 30; 40 50 60; 70 80 90];\r\ndiag_correct =  [10 50 90];\r\nantidiag_correct = [ 70    50    30];\r\n[A,B]= your_fcn_name(x);\r\nassert(isequal(A,diag_correct))\r\nassert(isequal(B,antidiag_correct))\r\n\r\n\r\n%% m-by-n matrix where m \u003e= n (2)\r\nx = [10 20 30; 40 50 60  ; 70 80 90; 100 74 15];\r\ndiag_corrct78 =  [10 50 90];\r\nantidiag_corrct78 = [ 100   80    60];\r\n[A3,B3]= your_fcn_name(x);\r\nassert(isequal(A3,diag_corrct78))\r\nassert(isequal(B3,antidiag_corrct78))\r\n\r\n\r\n%% m-by-n matrix where m \u003c= n (3)\r\nx = [10 20 30 58; 40 50 60 68 ; 70 80 90 98];\r\ndiag_correct7 =  [10 50 90];\r\nantidiag_correct7 = [ 70    50    30];\r\n[A3,B3]= your_fcn_name(x);\r\nassert(isequal(A3,diag_correct7))\r\nassert(isequal(B3,antidiag_correct7))\r\n\r\n%% m-by-n matrix where m \u003e= n (4)\r\nt = [0 0 0; 1 0 0 ; 0  2 0; 0 0 3];\r\ndiag8 =  [0 0 0];\r\nantid7 = [ 0 2 0];\r\n[A3,B3]= your_fcn_name(t);\r\nassert(isequal(A3,diag8))\r\nassert(isequal(B3,antid7))\r\n\r\n\r\n% m-by-n matrix where m \u003c= n (5)\r\nt = [0 1 0 0; 0 0 2 0 ; 0 0 0 3];\r\ndiag8 =  [0 0 0];\r\nantid7 = [ 0 0 0];\r\n[A3,B3]= your_fcn_name(t);\r\nassert(isequal(A3,diag8))\r\nassert(isequal(B3,antid7))","published":true,"deleted":false,"likes_count":11,"comments_count":2,"created_by":639,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":513,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":12,"created_at":"2012-07-24T07:26:53.000Z","updated_at":"2026-02-10T12:04:49.000Z","published_at":"2012-07-24T07:26:53.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\u003eIn the problem\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://www.mathworks.com/matlabcentral/cody/problems/858-permute-diagonal-and-antidiagonal\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eProblem 858. Permute diagonal and antidiagonal\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e created by\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://www.mathworks.com/matlabcentral/cody/players/343082-jean-marie-sainthillier\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eJean-Marie SAINTHILLIER\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e the test suite is only based on squared m-by-m matrix .\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 this problem you must submit a solution which\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\u003ereturns the diagonal and anti-diagonal elements without using the DIAG function\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e for any\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\u003em-by-n matrix\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=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eIf the input is a matrix 4*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[x =  [ 0     0     0\\n       1    48    17\\n       0     2    99\\n       14    0    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\u003ewe expect\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\u003e2 output vectors\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[dg_elements = [0 48 99] % diagonal elements\\n\\nantidg_elements = [14 2 17 ] % anti-diagonal elements]]\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 : You do not have to permute anything. Just return the elements.\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":2587,"title":"Find indices of diagonal elements","description":"Given a matrix A  and a diagonal index d (where 0 indicates the main diagonal and off-diagonals are identified by positive and negative integers), return the indices of the elements of this diagonal. \r\n\r\nd is always correct but A can contain repetitions, so ...\r\n","description_html":"\u003cp\u003eGiven a matrix A  and a diagonal index d (where 0 indicates the main diagonal and off-diagonals are identified by positive and negative integers), return the indices of the elements of this diagonal.\u003c/p\u003e\u003cp\u003ed is always correct but A can contain repetitions, so ...\u003c/p\u003e","function_template":"function y = diag_indices(A,d)\r\n  y = d;\r\nend","test_suite":"%%\r\nx = [1 2 3;4 5 6;7 8 9];\r\na = [1 5 9];\r\nassert(isequal(diag_indices(x,0),a))\r\n%%\r\nx = magic(5);\r\nb = 5;\r\nassert(isequal(diag_indices(x,-4),b))\r\n%%\r\nx = magic(5);\r\na = 21;\r\nassert(isequal(diag_indices(x,4),a))\r\n%%\r\nx = magic(5);\r\nz = 5;\r\nassert(isequal(diag_indices(x,-4),z))\r\n%%\r\nx = magic(6);\r\na = [19 26 33];\r\nassert(isequal(diag_indices(x,3),a))\r\n%%\r\nx1 = magic(50);\r\ny_correct = 50;\r\nassert(isequal(diag_indices(x1,-49),y_correct))\r\n%%\r\nx2 = magic(50);\r\ny_correct = [49 100];\r\nassert(isequal(diag_indices(x2,-48),y_correct))\r\n%%\r\ny = magic(50);\r\nv = [48 99 150];\r\nassert(isequal(diag_indices(y,-47),v))\r\n%%\r\nv=[1 2 3;4 5 6;1 2 3];\r\nt = [1 5 9];\r\nassert(isequal(diag_indices(v,0),t))\r\n%%\r\nx=[1 2;1 2];\r\ny_correct = 3;\r\nassert(isequal(diag_indices(x,1),y_correct))\r\n%%\r\nx=[1 1 1;1 1 1;1 1 1];\r\nt =  [1 5 9];\r\nassert(isequal(diag_indices(x,0),t))\r\n%%\r\ny=[1 2 3;1 2 3];\r\ny_correct = [1 4];\r\nassert(isequal(diag_indices(y,0),y_correct))\r\n","published":true,"deleted":false,"likes_count":2,"comments_count":2,"created_by":5390,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":73,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":39,"created_at":"2014-09-15T11:37:21.000Z","updated_at":"2026-04-02T08:13:56.000Z","published_at":"2014-09-15T11:38: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\",\"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\u003eGiven a matrix A and a diagonal index d (where 0 indicates the main diagonal and off-diagonals are identified by positive and negative integers), return the indices of the elements of this 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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ed is always correct but A can contain repetitions, so ...\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":45578,"title":"Create a matrix that counts up diagonally","description":"Given a single input _N_, create a _N_ x _N_ matrix that counts from 1 : _N_ ², (along up-right diagonals, starting with 1 in the top left corner. For example, given N=4...\r\n\r\n  1  3  6 10\r\n  2  5  9 13 \r\n  4  8 12 15 \r\n  7 11 14 16\r\n  \r\n\r\nNotice as you move up a row and right a column (↗) the values always increase by one. The value '1' should always go in the top left corner, with '2' directly below it. From there fill in the upward-rightward diagonals with the next higher integer until the matrix is complete. Assume N will always be a positive integer greater than 1 (N \u003e= 2).\r\n","description_html":"\u003cp\u003eGiven a single input \u003ci\u003eN\u003c/i\u003e, create a \u003ci\u003eN\u003c/i\u003e x \u003ci\u003eN\u003c/i\u003e matrix that counts from 1 : \u003ci\u003eN\u003c/i\u003e ², (along up-right diagonals, starting with 1 in the top left corner. For example, given N=4...\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e1  3  6 10\r\n2  5  9 13 \r\n4  8 12 15 \r\n7 11 14 16\r\n\u003c/pre\u003e\u003cp\u003eNotice as you move up a row and right a column (↗) the values always increase by one. The value '1' should always go in the top left corner, with '2' directly below it. From there fill in the upward-rightward diagonals with the next higher integer until the matrix is complete. Assume N will always be a positive integer greater than 1 (N \u0026gt;= 2).\u003c/p\u003e","function_template":"function mx = not_hankel(N)\r\n\r\n    mx = ones(N); %replace with your code\r\n\r\nend","test_suite":"%%\r\nN = 20;\r\n\r\nmx_correct = [...\r\n     1     3     6    10    15    21    28    36    45    55    66    78    91   105   120   136   153   171   190   210\r\n     2     5     9    14    20    27    35    44    54    65    77    90   104   119   135   152   170   189   209   229\r\n     4     8    13    19    26    34    43    53    64    76    89   103   118   134   151   169   188   208   228   247\r\n     7    12    18    25    33    42    52    63    75    88   102   117   133   150   168   187   207   227   246   264\r\n    11    17    24    32    41    51    62    74    87   101   116   132   149   167   186   206   226   245   263   280\r\n    16    23    31    40    50    61    73    86   100   115   131   148   166   185   205   225   244   262   279   295\r\n    22    30    39    49    60    72    85    99   114   130   147   165   184   204   224   243   261   278   294   309\r\n    29    38    48    59    71    84    98   113   129   146   164   183   203   223   242   260   277   293   308   322\r\n    37    47    58    70    83    97   112   128   145   163   182   202   222   241   259   276   292   307   321   334\r\n    46    57    69    82    96   111   127   144   162   181   201   221   240   258   275   291   306   320   333   345\r\n    56    68    81    95   110   126   143   161   180   200   220   239   257   274   290   305   319   332   344   355\r\n    67    80    94   109   125   142   160   179   199   219   238   256   273   289   304   318   331   343   354   364\r\n    79    93   108   124   141   159   178   198   218   237   255   272   288   303   317   330   342   353   363   372\r\n    92   107   123   140   158   177   197   217   236   254   271   287   302   316   329   341   352   362   371   379\r\n   106   122   139   157   176   196   216   235   253   270   286   301   315   328   340   351   361   370   378   385\r\n   121   138   156   175   195   215   234   252   269   285   300   314   327   339   350   360   369   377   384   390\r\n   137   155   174   194   214   233   251   268   284   299   313   326   338   349   359   368   376   383   389   394\r\n   154   173   193   213   232   250   267   283   298   312   325   337   348   358   367   375   382   388   393   397\r\n   172   192   212   231   249   266   282   297   311   324   336   347   357   366   374   381   387   392   396   399\r\n   191   211   230   248   265   281   296   310   323   335   346   356   365   373   380   386   391   395   398   400\r\n   ];\r\n\r\nassert(isequal(not_hankel(N),mx_correct))\r\n\r\n\r\n\r\n%%\r\nN = 3;\r\n\r\nmx_correct = [...\r\n     1     3     6\r\n     2     5     8\r\n     4     7     9\r\n     ];\r\n\r\nassert(isequal(not_hankel(N),mx_correct))\r\n\r\n\r\n\r\n%%\r\nrng('shuffle')\r\nN  = randi(99)+5;\r\nr = repmat((0:(N-1))',1,N) + (0:(N-1));\r\np = ((.5.*r.^2 + .5.*r) + (r(1,:)+1));\r\nq = rot90(hankel(fliplr(0:N-1)),2).^2;\r\nmx_correct = p - q;\r\nassert(isequal(not_hankel(N),mx_correct))\r\n\r\n\r\n\r\n%% \r\nN = 2;\r\nmx_correct =...\r\n[...\r\n    1  3\r\n    2  4 \r\n];\r\n\r\nassert(isequal(not_hankel(N),mx_correct))","published":true,"deleted":false,"likes_count":2,"comments_count":0,"created_by":18354,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":22,"test_suite_updated_at":"2020-05-22T17:05:13.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2020-05-22T16:46:56.000Z","updated_at":"2026-01-20T13:24:26.000Z","published_at":"2020-05-22T16:46:56.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 single input\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\u003eN\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, create a\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\u003eN\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e x\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\u003eN\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e matrix that counts from 1 :\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\u003eN\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e ², (along up-right diagonals, starting with 1 in the top left corner. For example, given N=4...\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  3  6 10\\n2  5  9 13 \\n4  8 12 15 \\n7 11 14 16]]\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\u003eNotice as you move up a row and right a column (↗) the values always increase by one. The value '1' should always go in the top left corner, with '2' directly below it. From there fill in the upward-rightward diagonals with the next higher integer until the matrix is complete. Assume N will always be a positive integer greater than 1 (N \u0026gt;= 2).\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":2455,"title":"Diagonal of a Spiral Matrix","description":"Write a function that will return same output as diag(spiral(n)). The only exception is that spiral and diag functions are not allowed in your function. Also, loops are not allowed.","description_html":"\u003cp\u003eWrite a function that will return same output as diag(spiral(n)). The only exception is that spiral and diag functions are not allowed in your function. Also, loops are not allowed.\u003c/p\u003e","function_template":"function y=digSpi(n)\r\n\r\n\r\n\r\n\r\nend","test_suite":"s = evalc('type digSpi');\r\n\r\nif ~isempty(regexp(s,'error'))\r\n    error('error using error');\r\nend\r\n\r\nif ~isempty(regexp(s,'assert'))\r\n    error('error using assert');\r\nend\r\n\r\nif ~isempty(regexp(s,'diag'))\r\n    error('error using diag');\r\nend\r\n\r\nif ~isempty(regexp(s,'spiral'))\r\n    error('error using spiral');\r\nend\r\n\r\nif ~isempty(regexp(s,'for'))\r\n    error('error using for');\r\nend\r\n\r\nif ~isempty(regexp(s,'while'))\r\n    error('error using while');\r\nend\r\n\r\n%%\r\n\r\na = diag(spiral(4));\r\nb = digSpi(4);\r\nassert(isequal(a,b));\r\n\r\n%%\r\n\r\na = diag(spiral(5));\r\nb = digSpi(5);\r\nassert(isequal(a,b));\r\n\r\n%%\r\n\r\na = diag(spiral(6));\r\nb = digSpi(6);\r\nassert(isequal(a,b));\r\n\r\n%%\r\n\r\na = diag(spiral(7));\r\nb = digSpi(7);\r\nassert(isequal(a,b));\r\n\r\n%%\r\n\r\na = diag(spiral(8));\r\nb = digSpi(8);\r\nassert(isequal(a,b));","published":true,"deleted":false,"likes_count":2,"comments_count":0,"created_by":17203,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":111,"test_suite_updated_at":"2014-07-21T18:30:07.000Z","rescore_all_solutions":false,"group_id":31,"created_at":"2014-07-20T08:15:09.000Z","updated_at":"2026-03-04T17:27:33.000Z","published_at":"2014-07-20T08:15:09.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\u003eWrite a function that will return same output as diag(spiral(n)). The only exception is that spiral and diag functions are not allowed in your function. Also, loops are not allowed.\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":59242,"title":"Construct numbers from antidiagonals of stacks of binary numbers","description":"A sequence is constructed in the following way: Express the numbers 1, 2, 3, 4, 5,… in binary and stack them, right-justified. Then read the numbers in upward sloping diagonals and convert to decimal. The first four binary numbers are 11, 110, 101, 100, and their decimal equivalents are 3, 6, 5, 4. \r\n0 0 0 1\r\n0 0 1 0 \r\n0 0 1 1 \r\n0 1 0 0\r\n0 1 0 1\r\n0 1 1 0 \r\nWrite a function to compute the th term in the sequence. ","description_html":"\u003cdiv style = \"text-align: start; line-height: 20.4333px; min-height: 0px; white-space: normal; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, monospace; font-style: normal; font-size: 14px; font-weight: 400; text-decoration: rgb(0, 0, 0); white-space: normal; \"\u003e\u003cdiv style=\"block-size: 273px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 407px 136.5px; transform-origin: 407px 136.5px; vertical-align: baseline; \"\u003e\u003cdiv style=\"block-size: 63px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 31.5px; text-align: left; transform-origin: 384px 31.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 384px 8px; transform-origin: 384px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eA sequence is constructed in the following way: Express the numbers 1, 2, 3, 4, 5,… in binary and stack them, right-justified. Then read the numbers in upward sloping diagonals and convert to decimal. The first four binary numbers are 11, 110, 101, 100, and their decimal equivalents are 3, 6, 5, 4. \u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 26.95px 8px; transform-origin: 26.95px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"font-family: Menlo, Monaco, Consolas, \u0026quot;Courier New\u0026quot;, monospace; \"\u003e0 0 0 1\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 30.8px 8px; transform-origin: 30.8px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"font-family: Menlo, Monaco, Consolas, \u0026quot;Courier New\u0026quot;, monospace; \"\u003e0 0 1 0 \u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 30.8px 8px; transform-origin: 30.8px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"font-family: Menlo, Monaco, Consolas, \u0026quot;Courier New\u0026quot;, monospace; \"\u003e0 0 1 1 \u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 26.95px 8px; transform-origin: 26.95px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"font-family: Menlo, Monaco, Consolas, \u0026quot;Courier New\u0026quot;, monospace; \"\u003e0 1 0 0\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 26.95px 8px; transform-origin: 26.95px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"font-family: Menlo, Monaco, Consolas, \u0026quot;Courier New\u0026quot;, monospace; \"\u003e0 1 0 1\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 30.8px 8px; transform-origin: 30.8px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"font-family: Menlo, Monaco, Consolas, \u0026quot;Courier New\u0026quot;, monospace; \"\u003e0 1 1 0 \u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 98.6583px 8px; transform-origin: 98.6583px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eWrite a function to compute the \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"font-family: \u0026quot;STIXGeneral\u0026quot;, \u0026quot;STIXGeneral-webfont\u0026quot;, serif; font-style: italic; font-weight: 400; color: rgb(0, 0, 0);\"\u003en\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 77.0083px 8px; transform-origin: 77.0083px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eth term in the sequence. \u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function y = slopingBinary(n)\r\n  y = diag(dec2bin(1:n)');\r\nend","test_suite":"%%\r\nn = 4;\r\ny_correct = 4;\r\nassert(isequal(slopingBinary(n),y_correct))\r\n\r\n%%\r\nn = 12;\r\ny_correct = 28;\r\nassert(isequal(slopingBinary(n),y_correct))\r\n\r\n%%\r\nn = 27;\r\ny_correct = 61;\r\nassert(isequal(slopingBinary(n),y_correct))\r\n\r\n%%\r\nn = 59;\r\ny_correct = 93;\r\nassert(isequal(slopingBinary(n),y_correct))\r\n\r\n%%\r\nn = 122;\r\ny_correct = 190;\r\nassert(isequal(slopingBinary(n),y_correct))\r\n\r\n%%\r\nn = 249;\r\ny_correct = 379;\r\nassert(isequal(slopingBinary(n),y_correct))\r\n\r\n%%\r\nn = 1015;\r\ny_correct = 1529;\r\nassert(isequal(slopingBinary(n),y_correct))\r\n\r\n%%\r\nn = 2038;\r\ny_correct = 3066;\r\nassert(isequal(slopingBinary(n),y_correct))\r\n\r\n%%\r\nn = 4085;\r\ny_correct = 6143;\r\nassert(isequal(slopingBinary(n),y_correct))\r\n\r\n%%\r\nn = 8180;\r\ny_correct = 12276;\r\nassert(isequal(slopingBinary(n),y_correct))\r\n\r\n%%\r\nn = 16537;\r\ny_correct = 16539;\r\nassert(isequal(slopingBinary(n),y_correct))\r\n\r\n%%\r\nn = 485373;\r\ny_correct = 485383;\r\nassert(isequal(slopingBinary(n),y_correct))\r\n\r\n%%\r\nn = 7341901;\r\ny_correct = 7341911;\r\nassert(isequal(slopingBinary(n),y_correct))\r\n\r\n%%\r\nn = 55988359;\r\ny_correct = 55988361;\r\nassert(isequal(slopingBinary(n),y_correct))\r\n\r\n%%\r\nn = 1073741824;\r\nassert(isequal(slopingBinary(n),n))\r\n\r\n%%\r\nn = 17179869184;\r\nassert(isequal(slopingBinary(n),n))\r\n\r\n%%\r\nassert(isequal(slopingBinary(flintmax/2),flintmax/2))\r\n\r\n%%\r\nn = 899423417;\r\nyyyy_correct = 899423465;\r\ny = slopingBinary(slopingBinary(slopingBinary(slopingBinary(n))));\r\nassert(isequal(y,yyyy_correct))\r\n\r\n%%\r\nfiletext = fileread('slopingBinary.m');\r\nillegal = contains(filetext, 'assignin') || contains(filetext, 'assert') || contains(filetext,'regexp'); \r\nassert(~illegal)","published":true,"deleted":false,"likes_count":1,"comments_count":2,"created_by":46909,"edited_by":46909,"edited_at":"2023-11-28T03:00:34.000Z","deleted_by":null,"deleted_at":null,"solvers_count":5,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2023-11-28T03:00:26.000Z","updated_at":"2023-11-28T03:00:34.000Z","published_at":"2023-11-28T03:00:34.000Z","restored_at":null,"restored_by":null,"spam":null,"simulink":false,"admin_reviewed":false,"description_opc":"{\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eA sequence is constructed in the following way: Express the numbers 1, 2, 3, 4, 5,… in binary and stack them, right-justified. Then read the numbers in upward sloping diagonals and convert to decimal. The first four binary numbers are 11, 110, 101, 100, and their decimal equivalents are 3, 6, 5, 4. \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:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e0 0 0 1\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e0 0 1 0 \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:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e0 0 1 1 \u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e0 1 0 0\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:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e0 1 0 1\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e0 1 1 0 \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\u003eWrite a function to compute the \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:customXml w:element=\\\"equation\\\"\u003e\u003cw:customXmlPr\u003e\u003cw:attr w:name=\\\"displayStyle\\\" w:val=\\\"false\\\"/\u003e\u003cw:attr w:name=\\\"altTextString\\\" w:val=\\\"n\\\"/\u003e\u003c/w:customXmlPr\u003e\u003cw:r\u003e\u003cw:t\u003en\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:customXml\u003e\u003cw:r\u003e\u003cw:t\u003eth term in the sequence. \u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\",\"relationship\":null}],\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"target\":\"/matlab/document.xml\",\"relationshipId\":\"rId1\"}]}"},{"id":2585,"title":"How to reverse spdiags?","description":"The spdiags function extracts all nonzero diagonals from the m-by-n matrix A.\r\n\r\nSo if A=[1 2 3;4 5 6;7 8 9],\r\n\r\nspdiags(A)=[7 4 1 0 0;0 8 5 2 0;0 0 9 6 3].\r\n\r\nBut if I give you spdiags(A), can you give me A ?","description_html":"\u003cdiv style = \"text-align: start; line-height: 20.44px; min-height: 0px; white-space: normal; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, monospace; font-style: normal; font-size: 14px; font-weight: 400; text-decoration: none solid rgb(0, 0, 0); white-space: normal; \"\u003e\u003cdiv style=\"block-size: 213px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 332px 106.5px; transform-origin: 332px 106.5px; vertical-align: baseline; \"\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 309px 10.5px; text-align: left; transform-origin: 309px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eThe spdiags function extracts all nonzero diagonals from the m-by-n matrix A.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 309px 10.5px; text-align: left; transform-origin: 309px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eSo if\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgb(247, 247, 247); block-size: 20px; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; margin-block-end: 10px; margin-block-start: 10px; margin-bottom: 10px; margin-inline-end: 3px; margin-inline-start: 3px; margin-left: 3px; margin-right: 3px; margin-top: 10px; perspective-origin: 329px 10px; transform-origin: 329px 10px; margin-left: 3px; margin-top: 10px; margin-bottom: 10px; margin-right: 3px; \"\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 0px 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e  A = [1 2 3;4 5 6;7 8 9]\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 10px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 10px; perspective-origin: 309px 10.5px; text-align: left; transform-origin: 309px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 10px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003ethen\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgb(247, 247, 247); block-size: 60px; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; margin-block-end: 10px; margin-block-start: 10px; margin-bottom: 10px; margin-inline-end: 3px; margin-inline-start: 3px; margin-left: 3px; margin-right: 3px; margin-top: 10px; perspective-origin: 329px 30px; transform-origin: 329px 30px; margin-left: 3px; margin-top: 10px; margin-bottom: 10px; margin-right: 3px; \"\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 329px 10px; transform-origin: 329px 10px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 0px 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e  spdiags(A) = [7 4 1 0 0;\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 329px 10px; transform-origin: 329px 10px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 0px 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e                0 8 5 2 0;\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 329px 10px; transform-origin: 329px 10px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 0px 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e                0 0 9 6 3]\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 10px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 10px; perspective-origin: 309px 10.5px; text-align: left; transform-origin: 309px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 10px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eBut if I give you spdiags(A), can you give me A ?\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function y = inv_spdiags(x)\r\n  y = x;\r\nend","test_suite":"%%\r\nx = spdiags([1 2 3;4 5 6;7 8 9]);\r\nassert(isequal(spdiags(inv_spdiags(x)),x))\r\n%%\r\nb = spdiags(eye(10));\r\nassert(isequal(spdiags(inv_spdiags(b)),b))\r\n%%\r\nx = spdiags([1 2 3;4 5 6]);\r\nassert(isequal(spdiags(inv_spdiags(x)),x))\r\n%%\r\ny = spdiags(magic(9));\r\nassert(isequal(spdiags(inv_spdiags(y)),y))\r\n%%\r\nx = spdiags(magic(5));\r\nassert(isequal(spdiags(inv_spdiags(x)),x))\r\n%%\r\nz = spdiags(ones(10));\r\nassert(isequal(spdiags(inv_spdiags(z)),z))\r\n%%\r\nx = spdiags(randi(10,5));\r\nassert(isequal(spdiags(inv_spdiags(x)),x))\r\n%%\r\nx = spdiags(randi(5,[3 2]));\r\nassert(isequal(spdiags(inv_spdiags(x)),x))\r\n%%\r\na = spdiags(zeros(5));\r\nassert(isequal(spdiags(inv_spdiags(a)),a))","published":true,"deleted":false,"likes_count":2,"comments_count":1,"created_by":5390,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":43,"test_suite_updated_at":"2014-09-14T14:43:49.000Z","rescore_all_solutions":false,"group_id":39,"created_at":"2014-09-14T14:39:28.000Z","updated_at":"2026-02-15T11:51:56.000Z","published_at":"2014-09-14T14:41:28.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe spdiags function extracts all nonzero diagonals from the m-by-n matrix 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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eSo if\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;4 5 6;7 8 9]]]\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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ethen\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[  spdiags(A) = [7 4 1 0 0;\\n                0 8 5 2 0;\\n                0 0 9 6 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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eBut if I give you spdiags(A), can you give me A ?\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\",\"relationship\":null}],\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"target\":\"/matlab/document.xml\",\"relationshipId\":\"rId1\"}]}"}],"term":"tag:\"diag\"","current_player_id":null,"fields":[{"name":"page","type":"integer","callback":null,"default":1,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"per_page","type":"integer","callback":null,"default":50,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"sort","type":"string","callback":null,"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"body","type":"text","callback":null,"default":"*:*","directive":null,"facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":false},{"name":"group","type":"string","callback":null,"default":null,"directive":"group","facet":true,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"difficulty_rating_bin","type":"string","callback":null,"default":null,"directive":"difficulty_rating_bin","facet":true,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"id","type":"integer","callback":null,"default":null,"directive":"id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"tag","type":"string","callback":null,"default":null,"directive":"tag","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"product","type":"string","callback":null,"default":null,"directive":"product","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"created_at","type":"timeframe","callback":{},"default":null,"directive":"created_at","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"profile_id","type":"integer","callback":null,"default":null,"directive":"author_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"created_by","type":"string","callback":null,"default":null,"directive":"author","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"player_id","type":"integer","callback":null,"default":null,"directive":"solver_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"player","type":"string","callback":null,"default":null,"directive":"solver","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"solvers_count","type":"integer","callback":null,"default":null,"directive":"solvers_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"comments_count","type":"integer","callback":null,"default":null,"directive":"comments_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"likes_count","type":"integer","callback":null,"default":null,"directive":"likes_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"leader_id","type":"integer","callback":null,"default":null,"directive":"leader_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"leading_solution","type":"integer","callback":null,"default":null,"directive":"leading_solution","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true}],"filters":[{"name":"asset_type","type":"string","callback":null,"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":"\"cody:problem\"","prepend":true},{"name":"profile_id","type":"integer","callback":{},"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":"author_id","static":null,"prepend":true}],"query":{"params":{"per_page":50,"term":"tag:\"diag\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"diag\"","","\"","diag","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007f5350341cc8\u003e":null,"#\u003cMathWorks::Search::Field:0x00007f5350341c28\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007f5350340a08\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007f5350342308\u003e":1,"#\u003cMathWorks::Search::Field:0x00007f5350342268\u003e":50,"#\u003cMathWorks::Search::Field:0x00007f5350341e08\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007f5350341d68\u003e":"tag:\"diag\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007f5350341d68\u003e":"tag:\"diag\""},"queried_facets":{}},"query_backend":{"connection":{"configuration":{"index_url":"http://index-op-v2/solr/","query_url":"http://search-op-v2/solr/","direct_access_index_urls":["http://index-op-v2/solr/"],"direct_access_query_urls":["http://search-op-v2/solr/"],"timeout":10,"vhost":"search","exchange":"search.topic","heartbeat":30,"pre_index_mode":false,"host":"rabbitmq-eks","port":5672,"username":"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":"tag:\"diag\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"diag\"","","\"","diag","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007f5350341cc8\u003e":null,"#\u003cMathWorks::Search::Field:0x00007f5350341c28\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007f5350340a08\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007f5350342308\u003e":1,"#\u003cMathWorks::Search::Field:0x00007f5350342268\u003e":50,"#\u003cMathWorks::Search::Field:0x00007f5350341e08\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007f5350341d68\u003e":"tag:\"diag\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007f5350341d68\u003e":"tag:\"diag\""},"queried_facets":{}},"options":{"fields":["id","difficulty_rating"]},"join":" "},"results":[{"id":46888,"difficulty_rating":"easy"},{"id":1158,"difficulty_rating":"easy-medium"},{"id":2365,"difficulty_rating":"easy-medium"},{"id":859,"difficulty_rating":"easy-medium"},{"id":2587,"difficulty_rating":"easy-medium"},{"id":858,"difficulty_rating":"medium"},{"id":45578,"difficulty_rating":"medium"},{"id":2455,"difficulty_rating":"medium"},{"id":59242,"difficulty_rating":"medium-hard"},{"id":2585,"difficulty_rating":"medium-hard"}]}}