{"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":43607,"title":"Logical array indexing - part 1","description":"Given an array |A| of size |p x q| , return an array |Y| of the same size such that the following conditions are satisfied.\r\n\r\n(1) The non-zero elements should be greater than a given value |'n'| .\r\n\r\n(2) These non-zero entries in |Y| should have the same values as their corresponding elements in |A|\r\n\r\nFor example: Given |A = [3 4 5 6 2 4 5 6 3 6]| and |n = 4| , return |Y = [0 0 5 6 0 0 5 6 0 6]|","description_html":"\u003cp\u003eGiven an array \u003ctt\u003eA\u003c/tt\u003e of size \u003ctt\u003ep x q\u003c/tt\u003e , return an array \u003ctt\u003eY\u003c/tt\u003e of the same size such that the following conditions are satisfied.\u003c/p\u003e\u003cp\u003e(1) The non-zero elements should be greater than a given value \u003ctt\u003e'n'\u003c/tt\u003e .\u003c/p\u003e\u003cp\u003e(2) These non-zero entries in \u003ctt\u003eY\u003c/tt\u003e should have the same values as their corresponding elements in \u003ctt\u003eA\u003c/tt\u003e\u003c/p\u003e\u003cp\u003eFor example: Given \u003ctt\u003eA = [3 4 5 6 2 4 5 6 3 6]\u003c/tt\u003e and \u003ctt\u003en = 4\u003c/tt\u003e , return \u003ctt\u003eY = [0 0 5 6 0 0 5 6 0 6]\u003c/tt\u003e\u003c/p\u003e","function_template":"function Y = find_elements(A,n)\r\n  Y = A;\r\nend","test_suite":"%%\r\nA = [3 4 5 6 2 4 5 6 3 6];\r\nn = 4;\r\ny_correct = [0 0 5 6 0 0 5 6 0 6];\r\nassert(isequal(find_elements(A,n),y_correct))\r\n\r\n\r\n%%\r\nA = [0; 6; 1; 7; 3; 5; 2; 4];\r\nn = 3;\r\ny_correct = [0; 6; 0; 7; 0; 5; 0; 4];\r\nassert(isequal(find_elements(A,n),y_correct))\r\n\r\n\r\n%%\r\nA = magic(4);\r\nn = 8;\r\ny_correct = [16 0 0 13; 0 11 10 0; 9 0 0 12; 0 14 15 0];\r\nassert(isequal(find_elements(A,n),y_correct))","published":true,"deleted":false,"likes_count":1,"comments_count":0,"created_by":70119,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":48,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2016-10-24T15:31:58.000Z","updated_at":"2026-03-02T14:23:54.000Z","published_at":"2016-10-24T15:31:58.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eGiven an array\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eA\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e of size\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ep x q\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e , return an array\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eY\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e of the same size such that the following conditions are satisfied.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e(1) The non-zero elements should be greater than a given value\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e'n'\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=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e(2) These non-zero entries in\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eY\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e should have the same values as their corresponding elements in\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eA\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eFor example: Given\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eA = [3 4 5 6 2 4 5 6 3 6]\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e and\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003en = 4\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e , return\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eY = [0 0 5 6 0 0 5 6 0 6]\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":42515,"title":"Remove All elements less than 5","description":"Given an input vector x, remove all elements of x less than 5 .\r\n\r\nExample:\r\n\r\n Input  x =  [  1 2 5 7 3 ]\r\n Output y is [  5 7 ]","description_html":"\u003cp\u003eGiven an input vector x, remove all elements of x less than 5 .\u003c/p\u003e\u003cp\u003eExample:\u003c/p\u003e\u003cpre\u003e Input  x =  [  1 2 5 7 3 ]\r\n Output y is [  5 7 ]\u003c/pre\u003e","function_template":"function y = removeElements(x)\r\n  y = x;\r\nend","test_suite":"%%\r\nx = [-1 2 5 8 4 0];\r\ny_correct = [5 8];\r\nassert(isequal(removeElements(x),y_correct))\r\n\r\n%%\r\nx = [-1 2 5 9 7 4 0];\r\ny_correct = [5 9 7];\r\nassert(isequal(removeElements(x),y_correct))\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":0,"created_by":48756,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":144,"test_suite_updated_at":"2015-08-19T08:57:24.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2015-08-18T19:54:30.000Z","updated_at":"2026-02-18T11:20:23.000Z","published_at":"2015-08-18T19:56:42.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eGiven an input vector x, remove all elements of x less than 5 .\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExample:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ Input  x =  [  1 2 5 7 3 ]\\n Output y is [  5 7 ]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":44450,"title":"Create a magic square matrix for a given odd integer","description":"A magic square of size 'N' is a matrix that satisfies the following criterias:\r\n\r\n# Dimension - NxN\r\n# Matrix should contain ALL the numbers between 1 to N^2\r\n# Sum of all rows or columns or diagonals should be same\r\n\r\nE.g: N=3\r\n\r\nOutput:\r\n(Sum of Row1 elem, Sum of Col1 elem, Sum of main diagonal elem, sum of anti-diagonal elem)\r\n\r\n15, 15, 15, 15\r\n\r\n(Note that row/col/diag/anti-diag sum should be same)","description_html":"\u003cp\u003eA magic square of size 'N' is a matrix that satisfies the following criterias:\u003c/p\u003e\u003col\u003e\u003cli\u003eDimension - NxN\u003c/li\u003e\u003cli\u003eMatrix should contain ALL the numbers between 1 to N^2\u003c/li\u003e\u003cli\u003eSum of all rows or columns or diagonals should be same\u003c/li\u003e\u003c/ol\u003e\u003cp\u003eE.g: N=3\u003c/p\u003e\u003cp\u003eOutput:\r\n(Sum of Row1 elem, Sum of Col1 elem, Sum of main diagonal elem, sum of anti-diagonal elem)\u003c/p\u003e\u003cp\u003e15, 15, 15, 15\u003c/p\u003e\u003cp\u003e(Note that row/col/diag/anti-diag sum should be same)\u003c/p\u003e","function_template":"function [row1Sum, col1Sum, diag1Sum, adiagSum] = MagicSquare(n)\r\n  row1Sum = sum(n);\r\n  col1Sum = sum(n);\r\n  diag1Sum = sum(n);\r\n  adiagSum = sum(n);\r\nend","test_suite":"%%\r\nn = 3;\r\ny_correct = [15 15 15 15]; %row sum, col sum, main diag sum, other diag sum\r\n[a b c d] = MagicSquare(n);\r\nassert(isequal([a b c d],y_correct))\r\n\r\n%%\r\nn = 5;\r\ny_correct = [65 65 65 65];  %row sum, col sum, main diag sum, other diag sum\r\n[a b c d] = MagicSquare(n);\r\nassert(isequal([a b c d],y_correct))\r\n\r\n%%\r\nn = 9;\r\ny_correct = [369 369 369 369];  %row sum, col sum, main diag sum, other diag sum\r\n[a b c d] = MagicSquare(n);\r\nassert(isequal([a b c d],y_correct))\r\n\r\n%%\r\nn = 15;\r\ny_correct = [1695 1695 1695 1695];  %row sum, col sum, main diag sum, other diag sum\r\n[a b c d] = MagicSquare(n);\r\nassert(isequal([a b c d],y_correct))\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":2,"created_by":161443,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":40,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2017-12-12T12:53:59.000Z","updated_at":"2026-03-18T14:37:11.000Z","published_at":"2017-12-13T07:28:31.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eA magic square of size 'N' is a matrix that satisfies the following criterias:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"2\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eDimension - NxN\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"2\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eMatrix should contain ALL the numbers between 1 to N^2\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"2\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eSum of all rows or columns or diagonals should be same\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\u003eE.g: N=3\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eOutput: (Sum of Row1 elem, Sum of Col1 elem, Sum of main diagonal elem, sum of anti-diagonal elem)\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\u003e15, 15, 15, 15\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e(Note that row/col/diag/anti-diag sum should be same)\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":694,"title":"Remove NaNs and numbers adjacent to NaNs","description":"The aim is to remove the elements before and after NaN occurrences inside a vector.\r\n\r\nFor example:\r\n\r\n x = [6 10 5 8 9 NaN 23 10 7 3 21 43 NaN 4 6 7 8]\r\n\r\nThe output y will be:\r\n\r\n y = [6 10 5 8 10 7 3 21 6 7 8]\r\n\r\nIf NaNs occur at the beginning or end then first two or last two values are removed.\r\n\r\n x=[NaN 1 2 3]  yields [2 3]\r\n\r\nNaN values will exist in every case.\r\nNo case is an empty set.","description_html":"\u003cp\u003eThe aim is to remove the elements before and after NaN occurrences inside a vector.\u003c/p\u003e\u003cp\u003eFor example:\u003c/p\u003e\u003cpre\u003e x = [6 10 5 8 9 NaN 23 10 7 3 21 43 NaN 4 6 7 8]\u003c/pre\u003e\u003cp\u003eThe output y will be:\u003c/p\u003e\u003cpre\u003e y = [6 10 5 8 10 7 3 21 6 7 8]\u003c/pre\u003e\u003cp\u003eIf NaNs occur at the beginning or end then first two or last two values are removed.\u003c/p\u003e\u003cpre\u003e x=[NaN 1 2 3]  yields [2 3]\u003c/pre\u003e\u003cp\u003eNaN values will exist in every case.\r\nNo case is an empty set.\u003c/p\u003e","function_template":"function y = NaN_window_delete(x)\r\n  y=x;\r\nend","test_suite":"%% \r\nx=[1 2 3 NaN 4 5 6];\r\ny_correct=[1 2 5 6];\r\nassert(isequal(NaN_window_delete(x),y_correct))\r\n%% \r\nx=[10 20 3 NaN 4 50 NaN 6];\r\ny_correct=[10 20];\r\nassert(isequal(NaN_window_delete(x),y_correct))\r\n%% \r\nx=[NaN 20 3 5 4 50 55 NaN];\r\ny_correct=[3 5 4 50];\r\nassert(isequal(NaN_window_delete(x),y_correct))\r\n%% \r\nx=[10 20 3 5 NaN 4 50 55 NaN 60 80 90 NaN 100 110 ];\r\ny_correct=[10 20 3 50 80 110];\r\nassert(isequal(NaN_window_delete(x),y_correct))\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":0,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":80,"test_suite_updated_at":"2012-05-16T21:35:19.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2012-05-16T21:24:52.000Z","updated_at":"2026-03-10T15:29:04.000Z","published_at":"2012-05-16T21:35:19.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe aim is to remove the elements before and after NaN occurrences inside a vector.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eFor example:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ x = [6 10 5 8 9 NaN 23 10 7 3 21 43 NaN 4 6 7 8]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe output y will be:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ y = [6 10 5 8 10 7 3 21 6 7 8]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eIf NaNs occur at the beginning or end then first two or last two values are removed.\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=[NaN 1 2 3]  yields [2 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\u003eNaN values will exist in every case. No case is an empty set.\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":640,"title":"Getting logical indexes","description":"This is a basic MATLAB operation.  It is for instructional purposes.\r\n\r\n---\r\n\r\nLogical indexing works like this.\r\n\r\n  thresh = 4;\r\n  vec    = [1 2 3 4 5 6 7 8];\r\n  \r\n  vi     = (vec \u003e thresh)\r\n  \r\n  vi =\r\n  \r\n       0     0     0     0     1     1     1     1\r\n\r\nOnce you have this TRUE FALSE vector (I call it vi: Valid Indices)\r\n\r\nIt can be used to get the values out:\r\n\r\n  big = vec(vi)\r\n  \r\n  big =\r\n  \r\n       5     6     7     8\r\n\r\nGiven a vector, vec, and a value, v, return a binary vector that represents the indices where vector, vec, is equal to scalar, v.\r\n\r\nNote, this works just as well with scalars and matrices.\r\n\r\n----\r\n\r\nTo get the indices where this comparison is true, see this \u003chttp://www.mathworks.com/matlabcentral/cody/problems/645-getting-the-indices-from-a-vector Cody problem\u003e.","description_html":"\u003cp\u003eThis is a basic MATLAB operation.  It is for instructional purposes.\u003c/p\u003e\u003cp\u003e---\u003c/p\u003e\u003cp\u003eLogical indexing works like this.\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003ethresh = 4;\r\nvec    = [1 2 3 4 5 6 7 8];\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003evi     = (vec \u003e thresh)\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003evi =\r\n\u003c/pre\u003e\u003cpre\u003e       0     0     0     0     1     1     1     1\u003c/pre\u003e\u003cp\u003eOnce you have this TRUE FALSE vector (I call it vi: Valid Indices)\u003c/p\u003e\u003cp\u003eIt can be used to get the values out:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003ebig = vec(vi)\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003ebig =\r\n\u003c/pre\u003e\u003cpre\u003e       5     6     7     8\u003c/pre\u003e\u003cp\u003eGiven a vector, vec, and a value, v, return a binary vector that represents the indices where vector, vec, is equal to scalar, v.\u003c/p\u003e\u003cp\u003eNote, this works just as well with scalars and matrices.\u003c/p\u003e\u003cp\u003e----\u003c/p\u003e\u003cp\u003eTo get the indices where this comparison is true, see this \u003ca href=\"http://www.mathworks.com/matlabcentral/cody/problems/645-getting-the-indices-from-a-vector\"\u003eCody problem\u003c/a\u003e.\u003c/p\u003e","function_template":"function vi = binaryEqualsVector(vec, v)\r\n  vi = true;\r\nend","test_suite":"%%\r\nvec = [1 2 3 3 2 1];\r\nv = 2;\r\ny_correct = [false true false false true false];\r\nassert(isequal(binaryEqualsVector(vec,v),y_correct))\r\n\r\n%%\r\nvec = [1 2 3 4 5 6];\r\nv = 0;\r\ny_correct = [false false false false false false];\r\nassert(isequal(binaryEqualsVector(vec,v),y_correct))\r\n\r\n%%\r\nvec = [1 1 1 1 1];\r\nv = 1;\r\ny_correct = [true true true true true];\r\nassert(isequal(binaryEqualsVector(vec,v),y_correct))\r\n\r\n%%\r\nvec = 'abcdef';\r\nv = 'a';\r\ny_correct = [true false false false false false];\r\nassert(isequal(binaryEqualsVector(vec,v),y_correct))\r\n","published":true,"deleted":false,"likes_count":9,"comments_count":0,"created_by":240,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":1182,"test_suite_updated_at":"2012-04-30T18:48:13.000Z","rescore_all_solutions":false,"group_id":12,"created_at":"2012-04-30T18:46:03.000Z","updated_at":"2026-02-10T12:14:38.000Z","published_at":"2012-04-30T18:48:13.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThis is a basic MATLAB operation. It is for instructional purposes.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e---\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eLogical indexing works like this.\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[thresh = 4;\\nvec    = [1 2 3 4 5 6 7 8];\\n\\nvi     = (vec \u003e thresh)\\n\\nvi =\\n\\n       0     0     0     0     1     1     1     1]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eOnce you have this TRUE FALSE vector (I call it vi: Valid Indices)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eIt can be used to get the values out:\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[big = vec(vi)\\n\\nbig =\\n\\n       5     6     7     8]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eGiven a vector, vec, and a value, v, return a binary vector that represents the indices where vector, vec, is equal to scalar, v.\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, this works just as well with scalars and matrices.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e----\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eTo get the indices where this comparison is true, see this\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/645-getting-the-indices-from-a-vector\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eCody problem\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":868,"title":"Replace all zeros and NaNs in a matrix with the string 'error'","description":"Given a numeric input matrix A, possibly containing some zero values and some NaNs, replace any occurrences of zero or NaN with the character string 'error'.\r\nThe output should be a cell array C of the same size as the input matrix. Each cell of the output cell array should either contain the corresponding entry of A if this is not zero or NaN, or the string 'error' otherwise.\r\n\r\nExample:\r\n\r\nIf A = [1 0; NaN 1], then the output C should be the cell array C = {1, 'error'; 'error', 1}.\r\n\r\n","description_html":"\u003cp\u003eGiven a numeric input matrix A, possibly containing some zero values and some NaNs, replace any occurrences of zero or NaN with the character string 'error'.\r\nThe output should be a cell array C of the same size as the input matrix. Each cell of the output cell array should either contain the corresponding entry of A if this is not zero or NaN, or the string 'error' otherwise.\u003c/p\u003e\u003cp\u003eExample:\u003c/p\u003e\u003cp\u003eIf A = [1 0; NaN 1], then the output C should be the cell array C = {1, 'error'; 'error', 1}.\u003c/p\u003e","function_template":"function C = replace_zeros_and_NaNs(A)\r\n\r\nend","test_suite":"%%\r\nA = 1;\r\nassert(isequal({1},replace_zeros_and_NaNs(A)))\r\n\r\n%%\r\nA = [1 0; NaN 1];\r\nC_correct = {1, 'error'; 'error', 1};\r\nassert(isequal(C_correct, replace_zeros_and_NaNs(A)))\r\n\r\n%% \r\nA = [];\r\nassert(isequal({}, replace_zeros_and_NaNs(A)))\r\n\r\n%%\r\nA = magic(5); A([14 3 5 6 7]) = 0; A([1 18 15 20 22]) = NaN;\r\nC_correct = {'error'    'error'   1  8 15; 23 'error' 7 14 'error';'error' 6 13 'error' 22; 10 12 'error' 21 3; 'error' 18 'error' 'error' 9};\r\nassert(isequal(C_correct, replace_zeros_and_NaNs(A)))\r\n\r\n","published":true,"deleted":false,"likes_count":0,"comments_count":2,"created_by":2328,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":103,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2012-07-28T18:25:06.000Z","updated_at":"2025-12-12T09:58:28.000Z","published_at":"2012-07-28T18:27:08.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 numeric input matrix A, possibly containing some zero values and some NaNs, replace any occurrences of zero or NaN with the character string 'error'. The output should be a cell array C of the same size as the input matrix. Each cell of the output cell array should either contain the corresponding entry of A if this is not zero or NaN, or the string 'error' otherwise.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExample:\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\u003eIf A = [1 0; NaN 1], then the output C should be the cell array C = {1, 'error'; 'error', 1}.\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":320,"title":"Maintain shape of logical-index mask","description":"When using logical indexing into an array, MATLAB returns a vector output. For example, if\r\n\r\n  A = magic(3);\r\n  \r\n  mask = logical([0 1 1; 0 1 1; 0 1 1]);\r\n\r\nthen\r\n\r\n  out=A(mask)\r\n\r\nreturns the output\r\n\r\n  out =\r\n  \r\n       1\r\n       5\r\n       9\r\n       6\r\n       7\r\n       2\r\n\r\nBut when the mask has a block format (as in this example), it is sometimes desirable to maintain that block shape in the output. So, in this case, the desired output would be\r\n\r\n  out =\r\n  \r\n       1   6\r\n       5   7\r\n       9   2\r\n\r\nIn other words, the output is the same shape as the logical index block.\r\n\r\nThe (first) input array _A_ will be two-dimensional, and the (second) input array _mask_ will be a logical that is guaranteed to have the block characteristic.","description_html":"\u003cp\u003eWhen using logical indexing into an array, MATLAB returns a vector output. For example, if\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eA = magic(3);\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003emask = logical([0 1 1; 0 1 1; 0 1 1]);\r\n\u003c/pre\u003e\u003cp\u003ethen\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eout=A(mask)\r\n\u003c/pre\u003e\u003cp\u003ereturns the output\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eout =\r\n\u003c/pre\u003e\u003cpre\u003e       1\r\n       5\r\n       9\r\n       6\r\n       7\r\n       2\u003c/pre\u003e\u003cp\u003eBut when the mask has a block format (as in this example), it is sometimes desirable to maintain that block shape in the output. So, in this case, the desired output would be\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eout =\r\n\u003c/pre\u003e\u003cpre\u003e       1   6\r\n       5   7\r\n       9   2\u003c/pre\u003e\u003cp\u003eIn other words, the output is the same shape as the logical index block.\u003c/p\u003e\u003cp\u003eThe (first) input array \u003ci\u003eA\u003c/i\u003e will be two-dimensional, and the (second) input array \u003ci\u003emask\u003c/i\u003e will be a logical that is guaranteed to have the block characteristic.\u003c/p\u003e","function_template":"function y = logicalBlock(A,mask)\r\n  y = A(mask); % This will make a vector, not a block\r\nend","test_suite":"%%\r\nA = magic(3);\r\nmask = logical([0 1 1; 0 1 1; 0 1 1]);\r\ny_correct = [1 6; 5 7; 9 2];\r\nassert(isequal(logicalBlock(A,mask),y_correct))\r\n%%\r\nA = magic(4);\r\nmask = logical([0 0 0 0; 0 1 1 0; 0 1 1 0; 0 0 0 0]);\r\ny_correct = [11 10; 7 6];\r\nassert(isequal(logicalBlock(A,mask),y_correct))\r\n%%\r\nA = magic(4);\r\nmask = logical([1 1 1 0; 1 1 1 0; 0 0 0 0; 0 0 0 0]);\r\ny_correct = [16 2 3; 5 11 10];\r\nassert(isequal(logicalBlock(A,mask),y_correct))\r\n%%\r\nA = magic(5);\r\nmask = logical(ones(5));\r\ny_correct = magic(5);\r\nassert(isequal(logicalBlock(A,mask),y_correct))","published":true,"deleted":false,"likes_count":2,"comments_count":3,"created_by":39,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":72,"test_suite_updated_at":"2014-02-13T12:44:37.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2012-02-15T15:51:45.000Z","updated_at":"2025-12-04T20:19:51.000Z","published_at":"2012-02-15T15:53:34.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\u003eWhen using logical indexing into an array, MATLAB returns a vector output. For example, 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 = magic(3);\\n\\nmask = logical([0 1 1; 0 1 1; 0 1 1]);]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\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[out=A(mask)]]\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\u003ereturns the 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[out =\\n\\n       1\\n       5\\n       9\\n       6\\n       7\\n       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\u003eBut when the mask has a block format (as in this example), it is sometimes desirable to maintain that block shape in the output. So, in this case, the desired output would be\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[out =\\n\\n       1   6\\n       5   7\\n       9   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\u003eIn other words, the output is the same shape as the logical index block.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe (first) input array\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eA\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e will be two-dimensional, and the (second) input array\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003emask\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e will be a logical that is guaranteed to have the block characteristic.\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":824,"title":" Set the array elements whose value is 13 to 0","description":"Input A either an array or a vector (which can be empty)\r\n\r\nOutput B will be the same size as A .\r\nAll elements of A equal to 13 (if any) must be replaced by zero.\r\n\r\nFor example:\r\n\r\n  A = [15 13 3]\r\n\r\noutput B is:\r\n\r\n  B= [15 0 3]\r\n\r\nThe difficulty in this problem is that the following commands *FOR , WHILE , FIND and IND2SUB cannot be used.*","description_html":"\u003cp\u003eInput A either an array or a vector (which can be empty)\u003c/p\u003e\u003cp\u003eOutput B will be the same size as A .\r\nAll elements of A equal to 13 (if any) must be replaced by zero.\u003c/p\u003e\u003cp\u003eFor example:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eA = [15 13 3]\r\n\u003c/pre\u003e\u003cp\u003eoutput B is:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eB= [15 0 3]\r\n\u003c/pre\u003e\u003cp\u003eThe difficulty in this problem is that the following commands \u003cb\u003eFOR , WHILE , FIND and IND2SUB cannot be used.\u003c/b\u003e\u003c/p\u003e","function_template":"function B = your_fcn_name(A)\r\n B = A;\r\nend","test_suite":"%% test 1\r\nA = [15 13 3];\r\ny_correct = [15 0 3];\r\nassert(isequal(your_fcn_name(A),y_correct))\r\nfiletext = fileread('your_fcn_name.m');\r\nassert(isempty(strfind(filetext, 'for')),'for command is forbidden')\r\nassert(isempty(strfind(filetext, 'while')),'while command is forbidden')\r\nassert(isempty(strfind(filetext, 'find')),'find command is forbidden')\r\nassert(isempty(strfind(filetext, 'ind2sub')),'ind2sub command is forbidden')\r\n\r\n%% test 2 \r\nA = 13;\r\ny_correct = 0;\r\nassert(isequal(your_fcn_name(A),y_correct))\r\nfiletext = fileread('your_fcn_name.m');\r\nassert(isempty(strfind(filetext, 'for')),'for command is forbidden')\r\nassert(isempty(strfind(filetext, 'while')),'while command is forbidden')\r\nassert(isempty(strfind(filetext, 'find')),'find command is forbidden')\r\nassert(isempty(strfind(filetext, 'ind2sub')),'ind2sub command is forbidden')\r\n\r\n%% test 3\r\nA = [];\r\ny_correct = [];\r\nassert(isequal(your_fcn_name(A),y_correct))\r\nfiletext = fileread('your_fcn_name.m');\r\nassert(isempty(strfind(filetext, 'for')),'for command is forbidden')\r\nassert(isempty(strfind(filetext, 'while')),'while command is forbidden')\r\nassert(isempty(strfind(filetext, 'find')),'find command is forbidden')\r\nassert(isempty(strfind(filetext, 'ind2sub')),'ind2sub command is forbidden')\r\n\r\n%% test 4\r\nA = [13 0 13; 13 13 8; 13 25 2];\r\ntiti = [0 0 0 ; 0 0 8;0 25 2];\r\n\r\nassert(isequal(your_fcn_name(A),titi))\r\n\r\nfiletext = fileread('your_fcn_name.m');\r\nassert(isempty(strfind(filetext, 'for')),'for command is forbidden')\r\nassert(isempty(strfind(filetext, 'while')),'while command is forbidden')\r\nassert(isempty(strfind(filetext, 'find')),'find command is forbidden')\r\nassert(isempty(strfind(filetext, 'ind2sub')),'ind2sub command is forbidden')\r\n\r\n\r\n%% test 5 with a  multidimensional array\r\nA=rand(10,10,10);\r\nA(1,2,2)=13;\r\nA(3,4,1)=13;\r\nA(5,6,3)=13;\r\n\r\n% what you cannot do but me I can ;) !\r\ntiti = A;\r\nI=find(titi==13);\r\n[u,v,w]=ind2sub(size(titi),I);\r\nfor ii = 1 : numel(u)\r\ntiti(u(ii),v(ii),w(ii))=0;\r\nend\r\nassert(isequal(your_fcn_name(A),titi))\r\nfiletext = fileread('your_fcn_name.m');\r\nassert(isempty(strfind(filetext, 'for')),'for command is forbidden')\r\nassert(isempty(strfind(filetext, 'while')),'while command is forbidden')\r\nassert(isempty(strfind(filetext, 'find')),'find command is forbidden')\r\nassert(isempty(strfind(filetext, 'ind2sub')),'ind2sub command is forbidden')\r\n\r\n%% test 6 repmat !\r\nA = repmat(13,89,17);\r\nassert(isequal(your_fcn_name(A),repmat(0,89,17)))\r\nfiletext = fileread('your_fcn_name.m');\r\nassert(isempty(strfind(filetext, 'for')),'for command is forbidden')\r\nassert(isempty(strfind(filetext, 'while')),'while command is forbidden')\r\nassert(isempty(strfind(filetext, 'find')),'find command is forbidden')\r\nassert(isempty(strfind(filetext, 'ind2sub')),'ind2sub command is forbidden')\r\n\r\n%% test 7 magic! \r\nA= magic(5);\r\nB =A;\r\nB(3,3) = 0\r\nassert(isequal(your_fcn_name(A),B))\r\nfiletext = fileread('your_fcn_name.m');\r\nassert(isempty(strfind(filetext, 'for')),'for command is forbidden')\r\nassert(isempty(strfind(filetext, 'while')),'while command is forbidden')\r\nassert(isempty(strfind(filetext, 'find')),'find command is forbidden')\r\nassert(isempty(strfind(filetext, 'ind2sub')),'ind2sub command is forbidden')\r\n\r\n%% test 8 floating (maybe people will work with string recognition)\r\nA= 13.3;\r\nB = A;\r\nassert(isequal(your_fcn_name(A),B))","published":true,"deleted":false,"likes_count":14,"comments_count":3,"created_by":639,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":1439,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":12,"created_at":"2012-07-11T08:14:09.000Z","updated_at":"2026-03-28T02:11:31.000Z","published_at":"2012-07-11T08:14: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\",\"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\u003eInput A either an array or a vector (which can be empty)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eOutput B will be the same size as A . All elements of A equal to 13 (if any) must be replaced by zero.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eFor example:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[A = [15 13 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\u003eoutput B is:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[B= [15 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\u003eThe difficulty in this problem is that the following commands\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\u003eFOR , WHILE , FIND and IND2SUB cannot be used.\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":1704,"title":"Triangular matrices in 3D array","description":"Given a 3D numeric array _x_, return an array _y_ of the same size in which all entries to the right of the main diagonal are zero for _y_(:,:,i).\r\n\r\n*Example*\r\n\r\nIf\r\n\r\n  x(:,:,1) = 1 2 3\r\n             4 5 6\r\n             7 8 9\r\n\r\n  x(:,:,2) = 1 4 7\r\n             2 5 8\r\n             3 6 9\r\n\r\n  x(:,:,3) = 1 2 3\r\n             1 2 3\r\n             1 2 3\r\n\r\nthen\r\n\r\n  y(:,:,1) = 1 0 0\r\n             4 5 0\r\n             7 8 9\r\n\r\n  y(:,:,2) = 1 0 0\r\n             2 5 0\r\n             3 6 9\r\n\r\n  y(:,:,3) = 1 0 0\r\n             1 2 0\r\n             1 2 3","description_html":"\u003cp\u003eGiven a 3D numeric array \u003ci\u003ex\u003c/i\u003e, return an array \u003ci\u003ey\u003c/i\u003e of the same size in which all entries to the right of the main diagonal are zero for \u003ci\u003ey\u003c/i\u003e(:,:,i).\u003c/p\u003e\u003cp\u003e\u003cb\u003eExample\u003c/b\u003e\u003c/p\u003e\u003cp\u003eIf\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003ex(:,:,1) = 1 2 3\r\n           4 5 6\r\n           7 8 9\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003ex(:,:,2) = 1 4 7\r\n           2 5 8\r\n           3 6 9\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003ex(:,:,3) = 1 2 3\r\n           1 2 3\r\n           1 2 3\r\n\u003c/pre\u003e\u003cp\u003ethen\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003ey(:,:,1) = 1 0 0\r\n           4 5 0\r\n           7 8 9\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003ey(:,:,2) = 1 0 0\r\n           2 5 0\r\n           3 6 9\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003ey(:,:,3) = 1 0 0\r\n           1 2 0\r\n           1 2 3\r\n\u003c/pre\u003e","function_template":"function y = tril3(x)\r\n  y = x;\r\nend","test_suite":"%%\r\nx(:,:,1) = [1 2 3;4 5 6;7 8 9];\r\nx(:,:,2) = [1 4 7;2 5 8;3 6 9];\r\nx(:,:,3) = [1 2 3;1 2 3;1 2 3];\r\ny_correct(:,:,1) = [1 0 0;4 5 0;7 8 9];\r\ny_correct(:,:,2) = [1 0 0;2 5 0;3 6 9];\r\ny_correct(:,:,3) = [1 0 0;1 2 0;1 2 3];\r\nassert(isequal(tril3(x),y_correct))\r\n\r\n%%\r\nx = cumsum(ones(3,3,50));\r\ny_correct = repmat([1 0 0;2 2 0; 3 3 3],[1,1,50]);\r\nassert(isequal(tril3(x),y_correct))\r\n\r\n%%\r\nx = cumsum(ones(5,5,100),2);\r\ny_correct = repmat(tril(cumsum(ones(5,5),2)),[1,1,100]);\r\nassert(isequal(tril3(x),y_correct))\r\n\r\n%%\r\nx = rand([1 1 400]);\r\ny_correct = x;\r\nassert(isequal(tril3(x),y_correct))\r\n\r\n%%\r\nx = reshape(1:40,[2,2,10]);\r\ny_correct(:,:,1) = [1 0;2 4];\r\ny_correct(:,:,2) = [5 0;6 8];\r\ny_correct(:,:,3) = [9 0;10 12];\r\ny_correct(:,:,4) = [13 0;14 16];\r\ny_correct(:,:,5) = [17 0;18 20];\r\ny_correct(:,:,6) = [21 0;22 24];\r\ny_correct(:,:,7) = [25 0;26 28];\r\ny_correct(:,:,8) = [29 0;30 32];\r\ny_correct(:,:,9) = [33 0;34 36];\r\ny_correct(:,:,10) = [37 0;38 40];\r\nassert(isequal(tril3(x),y_correct))\r\n\r\n%%\r\nx = randi([0 1],[4,4,5]);\r\ny_correct(:,:,1) = tril(x(:,:,1));\r\ny_correct(:,:,2) = tril(x(:,:,2));\r\ny_correct(:,:,3) = tril(x(:,:,3));\r\ny_correct(:,:,4) = tril(x(:,:,4));\r\ny_correct(:,:,5) = tril(x(:,:,5));\r\nassert(isequal(tril3(x),y_correct))","published":true,"deleted":false,"likes_count":2,"comments_count":0,"created_by":4793,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":169,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":31,"created_at":"2013-07-09T14:02:30.000Z","updated_at":"2026-03-21T07:18:04.000Z","published_at":"2013-07-09T14:02:30.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 3D numeric array\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ex\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, return an array\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ey\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e of the same size in which all entries to the right of the main diagonal are zero for\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\u003ey\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e(:,:,i).\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\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\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(:,:,1) = 1 2 3\\n           4 5 6\\n           7 8 9\\n\\nx(:,:,2) = 1 4 7\\n           2 5 8\\n           3 6 9\\n\\nx(:,:,3) = 1 2 3\\n           1 2 3\\n           1 2 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\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[y(:,:,1) = 1 0 0\\n           4 5 0\\n           7 8 9\\n\\ny(:,:,2) = 1 0 0\\n           2 5 0\\n           3 6 9\\n\\ny(:,:,3) = 1 0 0\\n           1 2 0\\n           1 2 3]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":856,"title":"Getting the indices from a matrix","description":"Inspired by Problem 645.\r\nGetting the indices from a matrix. \r\nGiven a matrix A (or a vector), return the indices (always in row) where A is greater than scalar, thresh. More instructive without FIND instruction.","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: 102px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 407px 51px; transform-origin: 407px 51px; 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: 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: 78.5px 8px; transform-origin: 78.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eInspired by Problem 645.\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: 105.5px 8px; transform-origin: 105.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eGetting the indices from a matrix. \u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 42px; 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 21px; text-align: left; transform-origin: 384px 21px; 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: 376px 8px; transform-origin: 376px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eGiven a matrix A (or a vector), return the indices (always in row) where A is greater than scalar, thresh. More instructive without FIND instruction.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function out = MatrixIndices(A, thresh)\r\n  out=thresh*A;\r\nend","test_suite":"%%\r\nx = [1 2 3;4 5 6;7 8 9];\r\nthresh=5;\r\ny_correct = [3 6 8 9];\r\nfiletext = fileread('MatrixIndices.m');\r\nassert(isequal(MatrixIndices(x, thresh),y_correct))\r\nassert(isempty(strfind(filetext, 'find')))\r\n%%\r\nx = 1:2:100;\r\nthresh=90;\r\ny_correct = [46 47 48 49 50];\r\nfiletext = fileread('MatrixIndices.m');\r\nassert(isequal(MatrixIndices(x, thresh),y_correct))\r\nassert(isempty(strfind(filetext, 'find')))\r\n%%\r\nx = 1:2:100';\r\nthresh=90;\r\ny_correct = [46 47 48 49 50];\r\nfiletext = fileread('MatrixIndices.m');\r\nassert(isequal(MatrixIndices(x, thresh),y_correct))\r\nassert(isempty(strfind(filetext, 'find')))\r\n","published":true,"deleted":false,"likes_count":8,"comments_count":0,"created_by":5390,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":721,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":12,"created_at":"2012-07-22T06:26:21.000Z","updated_at":"2026-04-03T18:19:06.000Z","published_at":"2012-07-22T06:26:21.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\u003eInspired by Problem 645.\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\u003eGetting the indices from a 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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eGiven a matrix A (or a vector), return the indices (always in row) where A is greater than scalar, thresh. More instructive without FIND instruction.\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":1972,"title":"Convert matrix to 3D array of triangular matrices","description":"Given a 2D numeric array x in which each column represents the vectorized form of an upper triangular matrix, return a 3D array y containing the concatenated triangular matrices.\r\n\r\n* If the size of the input matrix x is MxN, then the size of the output matrix y is PxPxN, where M = sum(1:P)\r\n* You may assume that P\u003c=100\r\n\r\n*Example*\r\n\r\nIf\r\n\r\n  x = 1  7 13\r\n      2  8 14\r\n      3  9 15\r\n      4 10 16\r\n      5 11 17\r\n      6 12 18\r\n\r\nthen\r\n\r\n  y(:,:,1) =  1  2  4\r\n              0  3  5\r\n              0  0  6\r\n\r\n  y(:,:,2) =  7  8 10\r\n              0  9 11\r\n              0  0 12\r\n\r\n  y(:,:,3) = 13 14 16\r\n              0 15 17\r\n              0  0 18\r\n\r\n_NOTE:_ If you are wondering why this seems like a strange task, it is inspired by a genotype-\u003ephenotype mapping I am doing in a genetic algorithm.","description_html":"\u003cp\u003eGiven a 2D numeric array x in which each column represents the vectorized form of an upper triangular matrix, return a 3D array y containing the concatenated triangular matrices.\u003c/p\u003e\u003cul\u003e\u003cli\u003eIf the size of the input matrix x is MxN, then the size of the output matrix y is PxPxN, where M = sum(1:P)\u003c/li\u003e\u003cli\u003eYou may assume that P\u0026lt;=100\u003c/li\u003e\u003c/ul\u003e\u003cp\u003e\u003cb\u003eExample\u003c/b\u003e\u003c/p\u003e\u003cp\u003eIf\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003ex = 1  7 13\r\n    2  8 14\r\n    3  9 15\r\n    4 10 16\r\n    5 11 17\r\n    6 12 18\r\n\u003c/pre\u003e\u003cp\u003ethen\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003ey(:,:,1) =  1  2  4\r\n            0  3  5\r\n            0  0  6\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003ey(:,:,2) =  7  8 10\r\n            0  9 11\r\n            0  0 12\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003ey(:,:,3) = 13 14 16\r\n            0 15 17\r\n            0  0 18\r\n\u003c/pre\u003e\u003cp\u003e\u003ci\u003eNOTE:\u003c/i\u003e If you are wondering why this seems like a strange task, it is inspired by a genotype-\u0026gt;phenotype mapping I am doing in a genetic algorithm.\u003c/p\u003e","function_template":"function y = mat2triu3(x)\r\n  y = x;\r\nend","test_suite":"%%\r\nx = 1:100;\r\ny_correct = shiftdim(x,-1);\r\nassert(isequal(mat2triu3(x),y_correct))\r\n\r\n%%\r\nx = reshape(1:15,3,[]);\r\ny_correct(:,:,1) = [1 2;0 3];\r\ny_correct(:,:,2) = [4 5;0 6];\r\ny_correct(:,:,3) = [7 8;0 9];\r\ny_correct(:,:,4) = [10 11;0 12];\r\ny_correct(:,:,5) = [13 14;0 15];\r\nassert(isequal(mat2triu3(x),y_correct))\r\n\r\n%%\r\nx = reshape(1:18,3,[])';\r\ny_correct(:,:,1) = [1 4 10; 0 7 13; 0 0 16];\r\ny_correct(:,:,2) = [2 5 11; 0 8 14; 0 0 17];\r\ny_correct(:,:,3) = [3 6 12; 0 9 15; 0 0 18];\r\nassert(isequal(mat2triu3(x),y_correct))\r\n\r\n%%\r\nx = randi(50,sum(1:100),22);\r\ny = mat2triu3(x);\r\nmask = (y~=0);\r\nxb = reshape(y(mask),[],size(y,3));\r\nassert(isequal(size(y),[100 100 22]))\r\nassert(isequal(x,xb))\r\n\r\n","published":true,"deleted":false,"likes_count":3,"comments_count":0,"created_by":4793,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":135,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":23,"created_at":"2013-11-07T22:49:47.000Z","updated_at":"2026-03-31T09:10:36.000Z","published_at":"2013-11-07T22:58:51.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 2D numeric array x in which each column represents the vectorized form of an upper triangular matrix, return a 3D array y containing the concatenated triangular matrices.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eIf the size of the input matrix x is MxN, then the size of the output matrix y is PxPxN, where M = sum(1:P)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYou may assume that P\u0026lt;=100\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\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\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 = 1  7 13\\n    2  8 14\\n    3  9 15\\n    4 10 16\\n    5 11 17\\n    6 12 18]]\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\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[y(:,:,1) =  1  2  4\\n            0  3  5\\n            0  0  6\\n\\ny(:,:,2) =  7  8 10\\n            0  9 11\\n            0  0 12\\n\\ny(:,:,3) = 13 14 16\\n            0 15 17\\n            0  0 18]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eNOTE:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e If you are wondering why this seems like a strange task, it is inspired by a genotype-\u0026gt;phenotype mapping I am doing in a genetic algorithm.\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":939,"title":"DNA Pattern Match:  Performance  Metric - Speed","description":"The Challenge is to Rapidly find matches of DNA sequences, Length=6, in a 1,800,000 long DNA file.\r\n\r\nAt \u003chttp://imacst.com/issues/volume-3issue-1/ IMACST\u003e the paper \u003chttp://imacst.com/web_documents/1202002.pdf An Intelligent and Efficient Matching Algorithm to Finding a DNA Pattern\u003e claimed an astounding time improvement from  9.94 seconds to 7.84 seconds, 21% time reduction, to match six segments of length 6 in a 1.8M long DNA file. Basic probability asserts 1.8M/4^6 * 6 = 2637 matches. The paper's test case produced 2346 matches. The method employed used text processing in C++. The paper's L=25 and L=50 cases will be later challenges.\r\n\r\nMatlab can achieve matching a six pattern set of L=6 in \u003c15 msec (i5/16GB). This is merely a 99.8% time reduction.\r\n\r\n\r\n*Challenge Description:*\r\nDNA is made of letters ACGT, \u003chttp://en.wikipedia.org/wiki/DNA wiki DNA\u003e, which for the purposes of this Matlab Cody Challenge are given values 0 thru 3. (ACGT= 0123)\r\n\r\n*Input:* [DNA, DNA_ID, Patterns]\r\n\r\n* DNA is a 1.8M long uint8 row vector of values 0 thru 3.\r\n* DNA_ID identifies the DNA segment being processed. Multiple calls using the same DNA_ID will be performed. The first call of a DNA_ID is not timed.\r\n* Patterns is an Nx6 uint8 array where each row corresponds to a search pattern.\r\n\r\n*Output:* Locations\r\n\r\nLocations of all start indices that match any of the patterns\r\n\r\n*Scoring:* Average Time (msec) for a block of L=6 patterns\r\n\r\nExample:\r\n\r\n* DNA= [0 1 2 3 3 2 1 0 1 2 3 3]\r\n* Pattern = [2 3 3 2 1 0]\r\n* Locations = [3]\r\n\r\n*Hints:*\r\n\r\n* Vectorization (Base 4 to Base 10 of 6 character words)\r\n* Bitshift/Reshape to create all words\r\n* Logical Indexing\r\n \r\n\r\nComing soon: Genome DNA sequencing of PhagePhix174 and Haempphilus Influenza","description_html":"\u003cp\u003eThe Challenge is to Rapidly find matches of DNA sequences, Length=6, in a 1,800,000 long DNA file.\u003c/p\u003e\u003cp\u003eAt \u003ca href=\"http://imacst.com/issues/volume-3issue-1/\"\u003eIMACST\u003c/a\u003e the paper \u003ca href=\"http://imacst.com/web_documents/1202002.pdf\"\u003eAn Intelligent and Efficient Matching Algorithm to Finding a DNA Pattern\u003c/a\u003e claimed an astounding time improvement from  9.94 seconds to 7.84 seconds, 21% time reduction, to match six segments of length 6 in a 1.8M long DNA file. Basic probability asserts 1.8M/4^6 * 6 = 2637 matches. The paper's test case produced 2346 matches. The method employed used text processing in C++. The paper's L=25 and L=50 cases will be later challenges.\u003c/p\u003e\u003cp\u003eMatlab can achieve matching a six pattern set of L=6 in \u0026lt;15 msec (i5/16GB). This is merely a 99.8% time reduction.\u003c/p\u003e\u003cp\u003e\u003cb\u003eChallenge Description:\u003c/b\u003e\r\nDNA is made of letters ACGT, \u003ca href=\"http://en.wikipedia.org/wiki/DNA\"\u003ewiki DNA\u003c/a\u003e, which for the purposes of this Matlab Cody Challenge are given values 0 thru 3. (ACGT= 0123)\u003c/p\u003e\u003cp\u003e\u003cb\u003eInput:\u003c/b\u003e [DNA, DNA_ID, Patterns]\u003c/p\u003e\u003cul\u003e\u003cli\u003eDNA is a 1.8M long uint8 row vector of values 0 thru 3.\u003c/li\u003e\u003cli\u003eDNA_ID identifies the DNA segment being processed. Multiple calls using the same DNA_ID will be performed. The first call of a DNA_ID is not timed.\u003c/li\u003e\u003cli\u003ePatterns is an Nx6 uint8 array where each row corresponds to a search pattern.\u003c/li\u003e\u003c/ul\u003e\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e Locations\u003c/p\u003e\u003cp\u003eLocations of all start indices that match any of the patterns\u003c/p\u003e\u003cp\u003e\u003cb\u003eScoring:\u003c/b\u003e Average Time (msec) for a block of L=6 patterns\u003c/p\u003e\u003cp\u003eExample:\u003c/p\u003e\u003cul\u003e\u003cli\u003eDNA= [0 1 2 3 3 2 1 0 1 2 3 3]\u003c/li\u003e\u003cli\u003ePattern = [2 3 3 2 1 0]\u003c/li\u003e\u003cli\u003eLocations = [3]\u003c/li\u003e\u003c/ul\u003e\u003cp\u003e\u003cb\u003eHints:\u003c/b\u003e\u003c/p\u003e\u003cul\u003e\u003cli\u003eVectorization (Base 4 to Base 10 of 6 character words)\u003c/li\u003e\u003cli\u003eBitshift/Reshape to create all words\u003c/li\u003e\u003cli\u003eLogical Indexing\u003c/li\u003e\u003c/ul\u003e\u003cp\u003eComing soon: Genome DNA sequencing of PhagePhix174 and Haempphilus Influenza\u003c/p\u003e","function_template":"function Locations = find_DNA(DNA, DNA_ID, Patterns)\r\n persistent DNA_ID_loaded  % add other DNA created variables\r\n Locations=[];\r\n if isempty(DNA_ID_loaded) || DNA_ID~=DNA_ID_loaded\r\n  % Perform initialization actions on DNA vector - optional\r\n  DNA_ID_loaded=DNA_ID;\r\n  \r\n end % DNA_ID Processing\r\n\r\n\r\n\r\n \r\nend\r\n\r\n\r\n\r\n\r\n","test_suite":"%%\r\nfeval(@assignin,'caller','score',0);\r\n%%\r\n% Functionality Section\r\nPass=1;\r\nw=6;\r\nDNA=randi(4,1,1800000,'uint8')-1;\r\nPatterns=randi(4,12,6,'uint8')-1; % Create more than needed\r\nPatterns=unique(Patterns,'rows','stable'); % Elim dupes but maintain random\r\nPatterns=Patterns(1:6,:); % Take first 6 random vectors\r\nPatterns=unique(Patterns,'rows'); % Place in order for easier debugging\r\n\r\nDNA_ID=1;\r\nLocations = find_DNA(DNA, DNA_ID, Patterns);\r\nif isempty(Locations),Pass=0;end\r\nif max(size(Locations))\u003c1024,Pass=0;end\r\nfor i=1:size(Locations,1)\r\n      Pass=Pass \u0026\u0026 ismember(DNA(Locations(i,end):Locations(i,end)+w-1),Patterns,'rows');\r\n    end\r\n\r\nDNA=randi(4,1,1800000,'uint8')-1;\r\nPatterns=randi(4,24,6,'uint8')-1; % Create more than needed\r\nPatterns=unique(Patterns,'rows','stable'); % Elim dupes but maintain random\r\nPatterns=Patterns(1:12,:); % Take first 6 random vectors\r\nPatterns=unique(Patterns,'rows'); % Place in order for easier debugging\r\n\r\nDNA_ID=2;\r\nLocations = find_DNA(DNA, DNA_ID, Patterns);\r\nif isempty(Locations),Pass=0;end\r\nif max(size(Locations))\u003c1024,Pass=0;end\r\nfor i=1:size(Locations,1)\r\n      Pass=Pass \u0026\u0026 ismember(DNA(Locations(i,end):Locations(i,end)+w-1),Patterns,'rows');\r\n    end\r\n\r\n\r\nassert(isequal(Pass,1))\r\n%%\r\n% Timing Section\r\nDNA=randi(4,1,1800000,'uint8')-1;\r\nPatterns=randi(4,12,6,'uint8')-1; % Create more than needed\r\nPatterns=unique(Patterns,'rows','stable'); % Elim dupes but maintain random\r\nPatterns=Patterns(1:6,:); % Take first 6 random vectors\r\nPatterns=unique(Patterns,'rows'); % Place in order for easier debugging\r\n\r\nDNA_ID=1;\r\nLocations = find_DNA(DNA, DNA_ID, Patterns);\r\nLocations = find_DNA(DNA, DNA_ID, Patterns);\r\n\r\nt0=clock;\r\nfor i=1:10\r\n Locations = find_DNA(DNA, DNA_ID, Patterns);\r\nend\r\ndt1=etime(clock,t0)*1000; % msec conversion\r\nfprintf('Your Set 1 Time = %i msec\\n',floor(dt1))\r\n\r\n\r\nPatterns=randi(4,24,6,'uint8')-1; % Create more than needed\r\nPatterns=unique(Patterns,'rows','stable'); % Elim dupes but maintain random\r\nPatterns=Patterns(1:12,:); % Take first 12 random vectors\r\nPatterns=unique(Patterns,'rows'); % Place in order for easier debugging\r\n\r\nt0=clock;\r\nfor i=1:10\r\n Locations = find_DNA(DNA, DNA_ID, Patterns);\r\nend\r\ndt2=etime(clock,t0)*1000; % msec conversion\r\nfprintf('Your Set 2 Time = %i msec\\n',floor(dt2))\r\n\r\n\r\n% New DNA Set\r\nDNA=randi(4,1,1800000,'uint8')-1;\r\nPatterns=randi(4,12,6,'uint8')-1; % Create more than needed\r\nPatterns=unique(Patterns,'rows','stable'); % Elim dupes but maintain random\r\nPatterns=Patterns(1:6,:); % Take first 6 random vectors\r\nPatterns=unique(Patterns,'rows'); % Place in order for easier debugging\r\n\r\nDNA_ID=2;\r\nLocations = find_DNA(DNA, DNA_ID, Patterns);\r\nLocations = find_DNA(DNA, DNA_ID, Patterns);\r\n\r\nt0=clock;\r\nfor i=1:10\r\n Locations = find_DNA(DNA, DNA_ID, Patterns);\r\nend\r\ndt3=etime(clock,t0)*1000; % msec conversion\r\nfprintf('Your Set 3 Time = %i msec\\n',floor(dt3))\r\n\r\n\r\nfeval(@assignin,'caller','score',min(400,floor(double(dt1+dt2+dt3)/30)));\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":0,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":9,"test_suite_updated_at":"2012-09-10T03:41:37.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2012-09-10T00:09:00.000Z","updated_at":"2025-11-21T09:02:33.000Z","published_at":"2012-09-10T03:40:23.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe Challenge is to Rapidly find matches of DNA sequences, Length=6, in a 1,800,000 long DNA file.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eAt\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://imacst.com/issues/volume-3issue-1/\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eIMACST\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e the paper\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://imacst.com/web_documents/1202002.pdf\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eAn Intelligent and Efficient Matching Algorithm to Finding a DNA Pattern\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e claimed an astounding time improvement from 9.94 seconds to 7.84 seconds, 21% time reduction, to match six segments of length 6 in a 1.8M long DNA file. Basic probability asserts 1.8M/4^6 * 6 = 2637 matches. The paper's test case produced 2346 matches. The method employed used text processing in C++. The paper's L=25 and L=50 cases will be later challenges.\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\u003eMatlab can achieve matching a six pattern set of L=6 in \u0026lt;15 msec (i5/16GB). This is merely a 99.8% time reduction.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eChallenge Description:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e DNA is made of letters ACGT,\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://en.wikipedia.org/wiki/DNA\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ewiki DNA\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e, which for the purposes of this Matlab Cody Challenge are given values 0 thru 3. (ACGT= 0123)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eInput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e [DNA, DNA_ID, Patterns]\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eDNA is a 1.8M long uint8 row vector of values 0 thru 3.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eDNA_ID identifies the DNA segment being processed. Multiple calls using the same DNA_ID will be performed. The first call of a DNA_ID is not timed.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ePatterns is an Nx6 uint8 array where each row corresponds to a search pattern.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eOutput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Locations\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\u003eLocations of all start indices that match any of the patterns\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eScoring:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Average Time (msec) for a block of L=6 patterns\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=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eDNA= [0 1 2 3 3 2 1 0 1 2 3 3]\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ePattern = [2 3 3 2 1 0]\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eLocations = [3]\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eHints:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eVectorization (Base 4 to Base 10 of 6 character words)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eBitshift/Reshape to create all words\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eLogical Indexing\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\u003eComing soon: Genome DNA sequencing of PhagePhix174 and Haempphilus Influenza\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"}],"problem_search":{"errors":[],"problems":[{"id":43607,"title":"Logical array indexing - part 1","description":"Given an array |A| of size |p x q| , return an array |Y| of the same size such that the following conditions are satisfied.\r\n\r\n(1) The non-zero elements should be greater than a given value |'n'| .\r\n\r\n(2) These non-zero entries in |Y| should have the same values as their corresponding elements in |A|\r\n\r\nFor example: Given |A = [3 4 5 6 2 4 5 6 3 6]| and |n = 4| , return |Y = [0 0 5 6 0 0 5 6 0 6]|","description_html":"\u003cp\u003eGiven an array \u003ctt\u003eA\u003c/tt\u003e of size \u003ctt\u003ep x q\u003c/tt\u003e , return an array \u003ctt\u003eY\u003c/tt\u003e of the same size such that the following conditions are satisfied.\u003c/p\u003e\u003cp\u003e(1) The non-zero elements should be greater than a given value \u003ctt\u003e'n'\u003c/tt\u003e .\u003c/p\u003e\u003cp\u003e(2) These non-zero entries in \u003ctt\u003eY\u003c/tt\u003e should have the same values as their corresponding elements in \u003ctt\u003eA\u003c/tt\u003e\u003c/p\u003e\u003cp\u003eFor example: Given \u003ctt\u003eA = [3 4 5 6 2 4 5 6 3 6]\u003c/tt\u003e and \u003ctt\u003en = 4\u003c/tt\u003e , return \u003ctt\u003eY = [0 0 5 6 0 0 5 6 0 6]\u003c/tt\u003e\u003c/p\u003e","function_template":"function Y = find_elements(A,n)\r\n  Y = A;\r\nend","test_suite":"%%\r\nA = [3 4 5 6 2 4 5 6 3 6];\r\nn = 4;\r\ny_correct = [0 0 5 6 0 0 5 6 0 6];\r\nassert(isequal(find_elements(A,n),y_correct))\r\n\r\n\r\n%%\r\nA = [0; 6; 1; 7; 3; 5; 2; 4];\r\nn = 3;\r\ny_correct = [0; 6; 0; 7; 0; 5; 0; 4];\r\nassert(isequal(find_elements(A,n),y_correct))\r\n\r\n\r\n%%\r\nA = magic(4);\r\nn = 8;\r\ny_correct = [16 0 0 13; 0 11 10 0; 9 0 0 12; 0 14 15 0];\r\nassert(isequal(find_elements(A,n),y_correct))","published":true,"deleted":false,"likes_count":1,"comments_count":0,"created_by":70119,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":48,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2016-10-24T15:31:58.000Z","updated_at":"2026-03-02T14:23:54.000Z","published_at":"2016-10-24T15:31:58.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eGiven an array\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eA\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e of size\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ep x q\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e , return an array\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eY\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e of the same size such that the following conditions are satisfied.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e(1) The non-zero elements should be greater than a given value\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e'n'\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=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e(2) These non-zero entries in\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eY\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e should have the same values as their corresponding elements in\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eA\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eFor example: Given\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eA = [3 4 5 6 2 4 5 6 3 6]\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e and\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003en = 4\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e , return\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eY = [0 0 5 6 0 0 5 6 0 6]\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":42515,"title":"Remove All elements less than 5","description":"Given an input vector x, remove all elements of x less than 5 .\r\n\r\nExample:\r\n\r\n Input  x =  [  1 2 5 7 3 ]\r\n Output y is [  5 7 ]","description_html":"\u003cp\u003eGiven an input vector x, remove all elements of x less than 5 .\u003c/p\u003e\u003cp\u003eExample:\u003c/p\u003e\u003cpre\u003e Input  x =  [  1 2 5 7 3 ]\r\n Output y is [  5 7 ]\u003c/pre\u003e","function_template":"function y = removeElements(x)\r\n  y = x;\r\nend","test_suite":"%%\r\nx = [-1 2 5 8 4 0];\r\ny_correct = [5 8];\r\nassert(isequal(removeElements(x),y_correct))\r\n\r\n%%\r\nx = [-1 2 5 9 7 4 0];\r\ny_correct = [5 9 7];\r\nassert(isequal(removeElements(x),y_correct))\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":0,"created_by":48756,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":144,"test_suite_updated_at":"2015-08-19T08:57:24.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2015-08-18T19:54:30.000Z","updated_at":"2026-02-18T11:20:23.000Z","published_at":"2015-08-18T19:56:42.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eGiven an input vector x, remove all elements of x less than 5 .\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExample:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ Input  x =  [  1 2 5 7 3 ]\\n Output y is [  5 7 ]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":44450,"title":"Create a magic square matrix for a given odd integer","description":"A magic square of size 'N' is a matrix that satisfies the following criterias:\r\n\r\n# Dimension - NxN\r\n# Matrix should contain ALL the numbers between 1 to N^2\r\n# Sum of all rows or columns or diagonals should be same\r\n\r\nE.g: N=3\r\n\r\nOutput:\r\n(Sum of Row1 elem, Sum of Col1 elem, Sum of main diagonal elem, sum of anti-diagonal elem)\r\n\r\n15, 15, 15, 15\r\n\r\n(Note that row/col/diag/anti-diag sum should be same)","description_html":"\u003cp\u003eA magic square of size 'N' is a matrix that satisfies the following criterias:\u003c/p\u003e\u003col\u003e\u003cli\u003eDimension - NxN\u003c/li\u003e\u003cli\u003eMatrix should contain ALL the numbers between 1 to N^2\u003c/li\u003e\u003cli\u003eSum of all rows or columns or diagonals should be same\u003c/li\u003e\u003c/ol\u003e\u003cp\u003eE.g: N=3\u003c/p\u003e\u003cp\u003eOutput:\r\n(Sum of Row1 elem, Sum of Col1 elem, Sum of main diagonal elem, sum of anti-diagonal elem)\u003c/p\u003e\u003cp\u003e15, 15, 15, 15\u003c/p\u003e\u003cp\u003e(Note that row/col/diag/anti-diag sum should be same)\u003c/p\u003e","function_template":"function [row1Sum, col1Sum, diag1Sum, adiagSum] = MagicSquare(n)\r\n  row1Sum = sum(n);\r\n  col1Sum = sum(n);\r\n  diag1Sum = sum(n);\r\n  adiagSum = sum(n);\r\nend","test_suite":"%%\r\nn = 3;\r\ny_correct = [15 15 15 15]; %row sum, col sum, main diag sum, other diag sum\r\n[a b c d] = MagicSquare(n);\r\nassert(isequal([a b c d],y_correct))\r\n\r\n%%\r\nn = 5;\r\ny_correct = [65 65 65 65];  %row sum, col sum, main diag sum, other diag sum\r\n[a b c d] = MagicSquare(n);\r\nassert(isequal([a b c d],y_correct))\r\n\r\n%%\r\nn = 9;\r\ny_correct = [369 369 369 369];  %row sum, col sum, main diag sum, other diag sum\r\n[a b c d] = MagicSquare(n);\r\nassert(isequal([a b c d],y_correct))\r\n\r\n%%\r\nn = 15;\r\ny_correct = [1695 1695 1695 1695];  %row sum, col sum, main diag sum, other diag sum\r\n[a b c d] = MagicSquare(n);\r\nassert(isequal([a b c d],y_correct))\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":2,"created_by":161443,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":40,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2017-12-12T12:53:59.000Z","updated_at":"2026-03-18T14:37:11.000Z","published_at":"2017-12-13T07:28:31.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eA magic square of size 'N' is a matrix that satisfies the following criterias:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"2\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eDimension - NxN\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"2\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eMatrix should contain ALL the numbers between 1 to N^2\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"2\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eSum of all rows or columns or diagonals should be same\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\u003eE.g: N=3\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eOutput: (Sum of Row1 elem, Sum of Col1 elem, Sum of main diagonal elem, sum of anti-diagonal elem)\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\u003e15, 15, 15, 15\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e(Note that row/col/diag/anti-diag sum should be same)\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":694,"title":"Remove NaNs and numbers adjacent to NaNs","description":"The aim is to remove the elements before and after NaN occurrences inside a vector.\r\n\r\nFor example:\r\n\r\n x = [6 10 5 8 9 NaN 23 10 7 3 21 43 NaN 4 6 7 8]\r\n\r\nThe output y will be:\r\n\r\n y = [6 10 5 8 10 7 3 21 6 7 8]\r\n\r\nIf NaNs occur at the beginning or end then first two or last two values are removed.\r\n\r\n x=[NaN 1 2 3]  yields [2 3]\r\n\r\nNaN values will exist in every case.\r\nNo case is an empty set.","description_html":"\u003cp\u003eThe aim is to remove the elements before and after NaN occurrences inside a vector.\u003c/p\u003e\u003cp\u003eFor example:\u003c/p\u003e\u003cpre\u003e x = [6 10 5 8 9 NaN 23 10 7 3 21 43 NaN 4 6 7 8]\u003c/pre\u003e\u003cp\u003eThe output y will be:\u003c/p\u003e\u003cpre\u003e y = [6 10 5 8 10 7 3 21 6 7 8]\u003c/pre\u003e\u003cp\u003eIf NaNs occur at the beginning or end then first two or last two values are removed.\u003c/p\u003e\u003cpre\u003e x=[NaN 1 2 3]  yields [2 3]\u003c/pre\u003e\u003cp\u003eNaN values will exist in every case.\r\nNo case is an empty set.\u003c/p\u003e","function_template":"function y = NaN_window_delete(x)\r\n  y=x;\r\nend","test_suite":"%% \r\nx=[1 2 3 NaN 4 5 6];\r\ny_correct=[1 2 5 6];\r\nassert(isequal(NaN_window_delete(x),y_correct))\r\n%% \r\nx=[10 20 3 NaN 4 50 NaN 6];\r\ny_correct=[10 20];\r\nassert(isequal(NaN_window_delete(x),y_correct))\r\n%% \r\nx=[NaN 20 3 5 4 50 55 NaN];\r\ny_correct=[3 5 4 50];\r\nassert(isequal(NaN_window_delete(x),y_correct))\r\n%% \r\nx=[10 20 3 5 NaN 4 50 55 NaN 60 80 90 NaN 100 110 ];\r\ny_correct=[10 20 3 50 80 110];\r\nassert(isequal(NaN_window_delete(x),y_correct))\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":0,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":80,"test_suite_updated_at":"2012-05-16T21:35:19.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2012-05-16T21:24:52.000Z","updated_at":"2026-03-10T15:29:04.000Z","published_at":"2012-05-16T21:35:19.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe aim is to remove the elements before and after NaN occurrences inside a vector.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eFor example:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ x = [6 10 5 8 9 NaN 23 10 7 3 21 43 NaN 4 6 7 8]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe output y will be:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ y = [6 10 5 8 10 7 3 21 6 7 8]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eIf NaNs occur at the beginning or end then first two or last two values are removed.\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=[NaN 1 2 3]  yields [2 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\u003eNaN values will exist in every case. No case is an empty set.\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":640,"title":"Getting logical indexes","description":"This is a basic MATLAB operation.  It is for instructional purposes.\r\n\r\n---\r\n\r\nLogical indexing works like this.\r\n\r\n  thresh = 4;\r\n  vec    = [1 2 3 4 5 6 7 8];\r\n  \r\n  vi     = (vec \u003e thresh)\r\n  \r\n  vi =\r\n  \r\n       0     0     0     0     1     1     1     1\r\n\r\nOnce you have this TRUE FALSE vector (I call it vi: Valid Indices)\r\n\r\nIt can be used to get the values out:\r\n\r\n  big = vec(vi)\r\n  \r\n  big =\r\n  \r\n       5     6     7     8\r\n\r\nGiven a vector, vec, and a value, v, return a binary vector that represents the indices where vector, vec, is equal to scalar, v.\r\n\r\nNote, this works just as well with scalars and matrices.\r\n\r\n----\r\n\r\nTo get the indices where this comparison is true, see this \u003chttp://www.mathworks.com/matlabcentral/cody/problems/645-getting-the-indices-from-a-vector Cody problem\u003e.","description_html":"\u003cp\u003eThis is a basic MATLAB operation.  It is for instructional purposes.\u003c/p\u003e\u003cp\u003e---\u003c/p\u003e\u003cp\u003eLogical indexing works like this.\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003ethresh = 4;\r\nvec    = [1 2 3 4 5 6 7 8];\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003evi     = (vec \u003e thresh)\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003evi =\r\n\u003c/pre\u003e\u003cpre\u003e       0     0     0     0     1     1     1     1\u003c/pre\u003e\u003cp\u003eOnce you have this TRUE FALSE vector (I call it vi: Valid Indices)\u003c/p\u003e\u003cp\u003eIt can be used to get the values out:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003ebig = vec(vi)\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003ebig =\r\n\u003c/pre\u003e\u003cpre\u003e       5     6     7     8\u003c/pre\u003e\u003cp\u003eGiven a vector, vec, and a value, v, return a binary vector that represents the indices where vector, vec, is equal to scalar, v.\u003c/p\u003e\u003cp\u003eNote, this works just as well with scalars and matrices.\u003c/p\u003e\u003cp\u003e----\u003c/p\u003e\u003cp\u003eTo get the indices where this comparison is true, see this \u003ca href=\"http://www.mathworks.com/matlabcentral/cody/problems/645-getting-the-indices-from-a-vector\"\u003eCody problem\u003c/a\u003e.\u003c/p\u003e","function_template":"function vi = binaryEqualsVector(vec, v)\r\n  vi = true;\r\nend","test_suite":"%%\r\nvec = [1 2 3 3 2 1];\r\nv = 2;\r\ny_correct = [false true false false true false];\r\nassert(isequal(binaryEqualsVector(vec,v),y_correct))\r\n\r\n%%\r\nvec = [1 2 3 4 5 6];\r\nv = 0;\r\ny_correct = [false false false false false false];\r\nassert(isequal(binaryEqualsVector(vec,v),y_correct))\r\n\r\n%%\r\nvec = [1 1 1 1 1];\r\nv = 1;\r\ny_correct = [true true true true true];\r\nassert(isequal(binaryEqualsVector(vec,v),y_correct))\r\n\r\n%%\r\nvec = 'abcdef';\r\nv = 'a';\r\ny_correct = [true false false false false false];\r\nassert(isequal(binaryEqualsVector(vec,v),y_correct))\r\n","published":true,"deleted":false,"likes_count":9,"comments_count":0,"created_by":240,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":1182,"test_suite_updated_at":"2012-04-30T18:48:13.000Z","rescore_all_solutions":false,"group_id":12,"created_at":"2012-04-30T18:46:03.000Z","updated_at":"2026-02-10T12:14:38.000Z","published_at":"2012-04-30T18:48:13.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThis is a basic MATLAB operation. It is for instructional purposes.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e---\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eLogical indexing works like this.\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[thresh = 4;\\nvec    = [1 2 3 4 5 6 7 8];\\n\\nvi     = (vec \u003e thresh)\\n\\nvi =\\n\\n       0     0     0     0     1     1     1     1]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eOnce you have this TRUE FALSE vector (I call it vi: Valid Indices)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eIt can be used to get the values out:\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[big = vec(vi)\\n\\nbig =\\n\\n       5     6     7     8]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eGiven a vector, vec, and a value, v, return a binary vector that represents the indices where vector, vec, is equal to scalar, v.\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, this works just as well with scalars and matrices.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e----\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eTo get the indices where this comparison is true, see this\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/645-getting-the-indices-from-a-vector\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eCody problem\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":868,"title":"Replace all zeros and NaNs in a matrix with the string 'error'","description":"Given a numeric input matrix A, possibly containing some zero values and some NaNs, replace any occurrences of zero or NaN with the character string 'error'.\r\nThe output should be a cell array C of the same size as the input matrix. Each cell of the output cell array should either contain the corresponding entry of A if this is not zero or NaN, or the string 'error' otherwise.\r\n\r\nExample:\r\n\r\nIf A = [1 0; NaN 1], then the output C should be the cell array C = {1, 'error'; 'error', 1}.\r\n\r\n","description_html":"\u003cp\u003eGiven a numeric input matrix A, possibly containing some zero values and some NaNs, replace any occurrences of zero or NaN with the character string 'error'.\r\nThe output should be a cell array C of the same size as the input matrix. Each cell of the output cell array should either contain the corresponding entry of A if this is not zero or NaN, or the string 'error' otherwise.\u003c/p\u003e\u003cp\u003eExample:\u003c/p\u003e\u003cp\u003eIf A = [1 0; NaN 1], then the output C should be the cell array C = {1, 'error'; 'error', 1}.\u003c/p\u003e","function_template":"function C = replace_zeros_and_NaNs(A)\r\n\r\nend","test_suite":"%%\r\nA = 1;\r\nassert(isequal({1},replace_zeros_and_NaNs(A)))\r\n\r\n%%\r\nA = [1 0; NaN 1];\r\nC_correct = {1, 'error'; 'error', 1};\r\nassert(isequal(C_correct, replace_zeros_and_NaNs(A)))\r\n\r\n%% \r\nA = [];\r\nassert(isequal({}, replace_zeros_and_NaNs(A)))\r\n\r\n%%\r\nA = magic(5); A([14 3 5 6 7]) = 0; A([1 18 15 20 22]) = NaN;\r\nC_correct = {'error'    'error'   1  8 15; 23 'error' 7 14 'error';'error' 6 13 'error' 22; 10 12 'error' 21 3; 'error' 18 'error' 'error' 9};\r\nassert(isequal(C_correct, replace_zeros_and_NaNs(A)))\r\n\r\n","published":true,"deleted":false,"likes_count":0,"comments_count":2,"created_by":2328,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":103,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2012-07-28T18:25:06.000Z","updated_at":"2025-12-12T09:58:28.000Z","published_at":"2012-07-28T18:27:08.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 numeric input matrix A, possibly containing some zero values and some NaNs, replace any occurrences of zero or NaN with the character string 'error'. The output should be a cell array C of the same size as the input matrix. Each cell of the output cell array should either contain the corresponding entry of A if this is not zero or NaN, or the string 'error' otherwise.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExample:\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\u003eIf A = [1 0; NaN 1], then the output C should be the cell array C = {1, 'error'; 'error', 1}.\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":320,"title":"Maintain shape of logical-index mask","description":"When using logical indexing into an array, MATLAB returns a vector output. For example, if\r\n\r\n  A = magic(3);\r\n  \r\n  mask = logical([0 1 1; 0 1 1; 0 1 1]);\r\n\r\nthen\r\n\r\n  out=A(mask)\r\n\r\nreturns the output\r\n\r\n  out =\r\n  \r\n       1\r\n       5\r\n       9\r\n       6\r\n       7\r\n       2\r\n\r\nBut when the mask has a block format (as in this example), it is sometimes desirable to maintain that block shape in the output. So, in this case, the desired output would be\r\n\r\n  out =\r\n  \r\n       1   6\r\n       5   7\r\n       9   2\r\n\r\nIn other words, the output is the same shape as the logical index block.\r\n\r\nThe (first) input array _A_ will be two-dimensional, and the (second) input array _mask_ will be a logical that is guaranteed to have the block characteristic.","description_html":"\u003cp\u003eWhen using logical indexing into an array, MATLAB returns a vector output. For example, if\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eA = magic(3);\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003emask = logical([0 1 1; 0 1 1; 0 1 1]);\r\n\u003c/pre\u003e\u003cp\u003ethen\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eout=A(mask)\r\n\u003c/pre\u003e\u003cp\u003ereturns the output\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eout =\r\n\u003c/pre\u003e\u003cpre\u003e       1\r\n       5\r\n       9\r\n       6\r\n       7\r\n       2\u003c/pre\u003e\u003cp\u003eBut when the mask has a block format (as in this example), it is sometimes desirable to maintain that block shape in the output. So, in this case, the desired output would be\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eout =\r\n\u003c/pre\u003e\u003cpre\u003e       1   6\r\n       5   7\r\n       9   2\u003c/pre\u003e\u003cp\u003eIn other words, the output is the same shape as the logical index block.\u003c/p\u003e\u003cp\u003eThe (first) input array \u003ci\u003eA\u003c/i\u003e will be two-dimensional, and the (second) input array \u003ci\u003emask\u003c/i\u003e will be a logical that is guaranteed to have the block characteristic.\u003c/p\u003e","function_template":"function y = logicalBlock(A,mask)\r\n  y = A(mask); % This will make a vector, not a block\r\nend","test_suite":"%%\r\nA = magic(3);\r\nmask = logical([0 1 1; 0 1 1; 0 1 1]);\r\ny_correct = [1 6; 5 7; 9 2];\r\nassert(isequal(logicalBlock(A,mask),y_correct))\r\n%%\r\nA = magic(4);\r\nmask = logical([0 0 0 0; 0 1 1 0; 0 1 1 0; 0 0 0 0]);\r\ny_correct = [11 10; 7 6];\r\nassert(isequal(logicalBlock(A,mask),y_correct))\r\n%%\r\nA = magic(4);\r\nmask = logical([1 1 1 0; 1 1 1 0; 0 0 0 0; 0 0 0 0]);\r\ny_correct = [16 2 3; 5 11 10];\r\nassert(isequal(logicalBlock(A,mask),y_correct))\r\n%%\r\nA = magic(5);\r\nmask = logical(ones(5));\r\ny_correct = magic(5);\r\nassert(isequal(logicalBlock(A,mask),y_correct))","published":true,"deleted":false,"likes_count":2,"comments_count":3,"created_by":39,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":72,"test_suite_updated_at":"2014-02-13T12:44:37.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2012-02-15T15:51:45.000Z","updated_at":"2025-12-04T20:19:51.000Z","published_at":"2012-02-15T15:53:34.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\u003eWhen using logical indexing into an array, MATLAB returns a vector output. For example, 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 = magic(3);\\n\\nmask = logical([0 1 1; 0 1 1; 0 1 1]);]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\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[out=A(mask)]]\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\u003ereturns the 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[out =\\n\\n       1\\n       5\\n       9\\n       6\\n       7\\n       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\u003eBut when the mask has a block format (as in this example), it is sometimes desirable to maintain that block shape in the output. So, in this case, the desired output would be\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[out =\\n\\n       1   6\\n       5   7\\n       9   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\u003eIn other words, the output is the same shape as the logical index block.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe (first) input array\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eA\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e will be two-dimensional, and the (second) input array\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003emask\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e will be a logical that is guaranteed to have the block characteristic.\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":824,"title":" Set the array elements whose value is 13 to 0","description":"Input A either an array or a vector (which can be empty)\r\n\r\nOutput B will be the same size as A .\r\nAll elements of A equal to 13 (if any) must be replaced by zero.\r\n\r\nFor example:\r\n\r\n  A = [15 13 3]\r\n\r\noutput B is:\r\n\r\n  B= [15 0 3]\r\n\r\nThe difficulty in this problem is that the following commands *FOR , WHILE , FIND and IND2SUB cannot be used.*","description_html":"\u003cp\u003eInput A either an array or a vector (which can be empty)\u003c/p\u003e\u003cp\u003eOutput B will be the same size as A .\r\nAll elements of A equal to 13 (if any) must be replaced by zero.\u003c/p\u003e\u003cp\u003eFor example:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eA = [15 13 3]\r\n\u003c/pre\u003e\u003cp\u003eoutput B is:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eB= [15 0 3]\r\n\u003c/pre\u003e\u003cp\u003eThe difficulty in this problem is that the following commands \u003cb\u003eFOR , WHILE , FIND and IND2SUB cannot be used.\u003c/b\u003e\u003c/p\u003e","function_template":"function B = your_fcn_name(A)\r\n B = A;\r\nend","test_suite":"%% test 1\r\nA = [15 13 3];\r\ny_correct = [15 0 3];\r\nassert(isequal(your_fcn_name(A),y_correct))\r\nfiletext = fileread('your_fcn_name.m');\r\nassert(isempty(strfind(filetext, 'for')),'for command is forbidden')\r\nassert(isempty(strfind(filetext, 'while')),'while command is forbidden')\r\nassert(isempty(strfind(filetext, 'find')),'find command is forbidden')\r\nassert(isempty(strfind(filetext, 'ind2sub')),'ind2sub command is forbidden')\r\n\r\n%% test 2 \r\nA = 13;\r\ny_correct = 0;\r\nassert(isequal(your_fcn_name(A),y_correct))\r\nfiletext = fileread('your_fcn_name.m');\r\nassert(isempty(strfind(filetext, 'for')),'for command is forbidden')\r\nassert(isempty(strfind(filetext, 'while')),'while command is forbidden')\r\nassert(isempty(strfind(filetext, 'find')),'find command is forbidden')\r\nassert(isempty(strfind(filetext, 'ind2sub')),'ind2sub command is forbidden')\r\n\r\n%% test 3\r\nA = [];\r\ny_correct = [];\r\nassert(isequal(your_fcn_name(A),y_correct))\r\nfiletext = fileread('your_fcn_name.m');\r\nassert(isempty(strfind(filetext, 'for')),'for command is forbidden')\r\nassert(isempty(strfind(filetext, 'while')),'while command is forbidden')\r\nassert(isempty(strfind(filetext, 'find')),'find command is forbidden')\r\nassert(isempty(strfind(filetext, 'ind2sub')),'ind2sub command is forbidden')\r\n\r\n%% test 4\r\nA = [13 0 13; 13 13 8; 13 25 2];\r\ntiti = [0 0 0 ; 0 0 8;0 25 2];\r\n\r\nassert(isequal(your_fcn_name(A),titi))\r\n\r\nfiletext = fileread('your_fcn_name.m');\r\nassert(isempty(strfind(filetext, 'for')),'for command is forbidden')\r\nassert(isempty(strfind(filetext, 'while')),'while command is forbidden')\r\nassert(isempty(strfind(filetext, 'find')),'find command is forbidden')\r\nassert(isempty(strfind(filetext, 'ind2sub')),'ind2sub command is forbidden')\r\n\r\n\r\n%% test 5 with a  multidimensional array\r\nA=rand(10,10,10);\r\nA(1,2,2)=13;\r\nA(3,4,1)=13;\r\nA(5,6,3)=13;\r\n\r\n% what you cannot do but me I can ;) !\r\ntiti = A;\r\nI=find(titi==13);\r\n[u,v,w]=ind2sub(size(titi),I);\r\nfor ii = 1 : numel(u)\r\ntiti(u(ii),v(ii),w(ii))=0;\r\nend\r\nassert(isequal(your_fcn_name(A),titi))\r\nfiletext = fileread('your_fcn_name.m');\r\nassert(isempty(strfind(filetext, 'for')),'for command is forbidden')\r\nassert(isempty(strfind(filetext, 'while')),'while command is forbidden')\r\nassert(isempty(strfind(filetext, 'find')),'find command is forbidden')\r\nassert(isempty(strfind(filetext, 'ind2sub')),'ind2sub command is forbidden')\r\n\r\n%% test 6 repmat !\r\nA = repmat(13,89,17);\r\nassert(isequal(your_fcn_name(A),repmat(0,89,17)))\r\nfiletext = fileread('your_fcn_name.m');\r\nassert(isempty(strfind(filetext, 'for')),'for command is forbidden')\r\nassert(isempty(strfind(filetext, 'while')),'while command is forbidden')\r\nassert(isempty(strfind(filetext, 'find')),'find command is forbidden')\r\nassert(isempty(strfind(filetext, 'ind2sub')),'ind2sub command is forbidden')\r\n\r\n%% test 7 magic! \r\nA= magic(5);\r\nB =A;\r\nB(3,3) = 0\r\nassert(isequal(your_fcn_name(A),B))\r\nfiletext = fileread('your_fcn_name.m');\r\nassert(isempty(strfind(filetext, 'for')),'for command is forbidden')\r\nassert(isempty(strfind(filetext, 'while')),'while command is forbidden')\r\nassert(isempty(strfind(filetext, 'find')),'find command is forbidden')\r\nassert(isempty(strfind(filetext, 'ind2sub')),'ind2sub command is forbidden')\r\n\r\n%% test 8 floating (maybe people will work with string recognition)\r\nA= 13.3;\r\nB = A;\r\nassert(isequal(your_fcn_name(A),B))","published":true,"deleted":false,"likes_count":14,"comments_count":3,"created_by":639,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":1439,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":12,"created_at":"2012-07-11T08:14:09.000Z","updated_at":"2026-03-28T02:11:31.000Z","published_at":"2012-07-11T08:14: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\",\"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\u003eInput A either an array or a vector (which can be empty)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eOutput B will be the same size as A . All elements of A equal to 13 (if any) must be replaced by zero.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eFor example:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[A = [15 13 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\u003eoutput B is:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[B= [15 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\u003eThe difficulty in this problem is that the following commands\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\u003eFOR , WHILE , FIND and IND2SUB cannot be used.\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":1704,"title":"Triangular matrices in 3D array","description":"Given a 3D numeric array _x_, return an array _y_ of the same size in which all entries to the right of the main diagonal are zero for _y_(:,:,i).\r\n\r\n*Example*\r\n\r\nIf\r\n\r\n  x(:,:,1) = 1 2 3\r\n             4 5 6\r\n             7 8 9\r\n\r\n  x(:,:,2) = 1 4 7\r\n             2 5 8\r\n             3 6 9\r\n\r\n  x(:,:,3) = 1 2 3\r\n             1 2 3\r\n             1 2 3\r\n\r\nthen\r\n\r\n  y(:,:,1) = 1 0 0\r\n             4 5 0\r\n             7 8 9\r\n\r\n  y(:,:,2) = 1 0 0\r\n             2 5 0\r\n             3 6 9\r\n\r\n  y(:,:,3) = 1 0 0\r\n             1 2 0\r\n             1 2 3","description_html":"\u003cp\u003eGiven a 3D numeric array \u003ci\u003ex\u003c/i\u003e, return an array \u003ci\u003ey\u003c/i\u003e of the same size in which all entries to the right of the main diagonal are zero for \u003ci\u003ey\u003c/i\u003e(:,:,i).\u003c/p\u003e\u003cp\u003e\u003cb\u003eExample\u003c/b\u003e\u003c/p\u003e\u003cp\u003eIf\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003ex(:,:,1) = 1 2 3\r\n           4 5 6\r\n           7 8 9\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003ex(:,:,2) = 1 4 7\r\n           2 5 8\r\n           3 6 9\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003ex(:,:,3) = 1 2 3\r\n           1 2 3\r\n           1 2 3\r\n\u003c/pre\u003e\u003cp\u003ethen\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003ey(:,:,1) = 1 0 0\r\n           4 5 0\r\n           7 8 9\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003ey(:,:,2) = 1 0 0\r\n           2 5 0\r\n           3 6 9\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003ey(:,:,3) = 1 0 0\r\n           1 2 0\r\n           1 2 3\r\n\u003c/pre\u003e","function_template":"function y = tril3(x)\r\n  y = x;\r\nend","test_suite":"%%\r\nx(:,:,1) = [1 2 3;4 5 6;7 8 9];\r\nx(:,:,2) = [1 4 7;2 5 8;3 6 9];\r\nx(:,:,3) = [1 2 3;1 2 3;1 2 3];\r\ny_correct(:,:,1) = [1 0 0;4 5 0;7 8 9];\r\ny_correct(:,:,2) = [1 0 0;2 5 0;3 6 9];\r\ny_correct(:,:,3) = [1 0 0;1 2 0;1 2 3];\r\nassert(isequal(tril3(x),y_correct))\r\n\r\n%%\r\nx = cumsum(ones(3,3,50));\r\ny_correct = repmat([1 0 0;2 2 0; 3 3 3],[1,1,50]);\r\nassert(isequal(tril3(x),y_correct))\r\n\r\n%%\r\nx = cumsum(ones(5,5,100),2);\r\ny_correct = repmat(tril(cumsum(ones(5,5),2)),[1,1,100]);\r\nassert(isequal(tril3(x),y_correct))\r\n\r\n%%\r\nx = rand([1 1 400]);\r\ny_correct = x;\r\nassert(isequal(tril3(x),y_correct))\r\n\r\n%%\r\nx = reshape(1:40,[2,2,10]);\r\ny_correct(:,:,1) = [1 0;2 4];\r\ny_correct(:,:,2) = [5 0;6 8];\r\ny_correct(:,:,3) = [9 0;10 12];\r\ny_correct(:,:,4) = [13 0;14 16];\r\ny_correct(:,:,5) = [17 0;18 20];\r\ny_correct(:,:,6) = [21 0;22 24];\r\ny_correct(:,:,7) = [25 0;26 28];\r\ny_correct(:,:,8) = [29 0;30 32];\r\ny_correct(:,:,9) = [33 0;34 36];\r\ny_correct(:,:,10) = [37 0;38 40];\r\nassert(isequal(tril3(x),y_correct))\r\n\r\n%%\r\nx = randi([0 1],[4,4,5]);\r\ny_correct(:,:,1) = tril(x(:,:,1));\r\ny_correct(:,:,2) = tril(x(:,:,2));\r\ny_correct(:,:,3) = tril(x(:,:,3));\r\ny_correct(:,:,4) = tril(x(:,:,4));\r\ny_correct(:,:,5) = tril(x(:,:,5));\r\nassert(isequal(tril3(x),y_correct))","published":true,"deleted":false,"likes_count":2,"comments_count":0,"created_by":4793,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":169,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":31,"created_at":"2013-07-09T14:02:30.000Z","updated_at":"2026-03-21T07:18:04.000Z","published_at":"2013-07-09T14:02:30.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 3D numeric array\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ex\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, return an array\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ey\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e of the same size in which all entries to the right of the main diagonal are zero for\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\u003ey\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e(:,:,i).\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\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\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(:,:,1) = 1 2 3\\n           4 5 6\\n           7 8 9\\n\\nx(:,:,2) = 1 4 7\\n           2 5 8\\n           3 6 9\\n\\nx(:,:,3) = 1 2 3\\n           1 2 3\\n           1 2 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\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[y(:,:,1) = 1 0 0\\n           4 5 0\\n           7 8 9\\n\\ny(:,:,2) = 1 0 0\\n           2 5 0\\n           3 6 9\\n\\ny(:,:,3) = 1 0 0\\n           1 2 0\\n           1 2 3]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":856,"title":"Getting the indices from a matrix","description":"Inspired by Problem 645.\r\nGetting the indices from a matrix. \r\nGiven a matrix A (or a vector), return the indices (always in row) where A is greater than scalar, thresh. More instructive without FIND instruction.","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: 102px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 407px 51px; transform-origin: 407px 51px; 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: 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: 78.5px 8px; transform-origin: 78.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eInspired by Problem 645.\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: 105.5px 8px; transform-origin: 105.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eGetting the indices from a matrix. \u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 42px; 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 21px; text-align: left; transform-origin: 384px 21px; 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: 376px 8px; transform-origin: 376px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eGiven a matrix A (or a vector), return the indices (always in row) where A is greater than scalar, thresh. More instructive without FIND instruction.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function out = MatrixIndices(A, thresh)\r\n  out=thresh*A;\r\nend","test_suite":"%%\r\nx = [1 2 3;4 5 6;7 8 9];\r\nthresh=5;\r\ny_correct = [3 6 8 9];\r\nfiletext = fileread('MatrixIndices.m');\r\nassert(isequal(MatrixIndices(x, thresh),y_correct))\r\nassert(isempty(strfind(filetext, 'find')))\r\n%%\r\nx = 1:2:100;\r\nthresh=90;\r\ny_correct = [46 47 48 49 50];\r\nfiletext = fileread('MatrixIndices.m');\r\nassert(isequal(MatrixIndices(x, thresh),y_correct))\r\nassert(isempty(strfind(filetext, 'find')))\r\n%%\r\nx = 1:2:100';\r\nthresh=90;\r\ny_correct = [46 47 48 49 50];\r\nfiletext = fileread('MatrixIndices.m');\r\nassert(isequal(MatrixIndices(x, thresh),y_correct))\r\nassert(isempty(strfind(filetext, 'find')))\r\n","published":true,"deleted":false,"likes_count":8,"comments_count":0,"created_by":5390,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":721,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":12,"created_at":"2012-07-22T06:26:21.000Z","updated_at":"2026-04-03T18:19:06.000Z","published_at":"2012-07-22T06:26:21.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\u003eInspired by Problem 645.\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\u003eGetting the indices from a 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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eGiven a matrix A (or a vector), return the indices (always in row) where A is greater than scalar, thresh. More instructive without FIND instruction.\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":1972,"title":"Convert matrix to 3D array of triangular matrices","description":"Given a 2D numeric array x in which each column represents the vectorized form of an upper triangular matrix, return a 3D array y containing the concatenated triangular matrices.\r\n\r\n* If the size of the input matrix x is MxN, then the size of the output matrix y is PxPxN, where M = sum(1:P)\r\n* You may assume that P\u003c=100\r\n\r\n*Example*\r\n\r\nIf\r\n\r\n  x = 1  7 13\r\n      2  8 14\r\n      3  9 15\r\n      4 10 16\r\n      5 11 17\r\n      6 12 18\r\n\r\nthen\r\n\r\n  y(:,:,1) =  1  2  4\r\n              0  3  5\r\n              0  0  6\r\n\r\n  y(:,:,2) =  7  8 10\r\n              0  9 11\r\n              0  0 12\r\n\r\n  y(:,:,3) = 13 14 16\r\n              0 15 17\r\n              0  0 18\r\n\r\n_NOTE:_ If you are wondering why this seems like a strange task, it is inspired by a genotype-\u003ephenotype mapping I am doing in a genetic algorithm.","description_html":"\u003cp\u003eGiven a 2D numeric array x in which each column represents the vectorized form of an upper triangular matrix, return a 3D array y containing the concatenated triangular matrices.\u003c/p\u003e\u003cul\u003e\u003cli\u003eIf the size of the input matrix x is MxN, then the size of the output matrix y is PxPxN, where M = sum(1:P)\u003c/li\u003e\u003cli\u003eYou may assume that P\u0026lt;=100\u003c/li\u003e\u003c/ul\u003e\u003cp\u003e\u003cb\u003eExample\u003c/b\u003e\u003c/p\u003e\u003cp\u003eIf\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003ex = 1  7 13\r\n    2  8 14\r\n    3  9 15\r\n    4 10 16\r\n    5 11 17\r\n    6 12 18\r\n\u003c/pre\u003e\u003cp\u003ethen\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003ey(:,:,1) =  1  2  4\r\n            0  3  5\r\n            0  0  6\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003ey(:,:,2) =  7  8 10\r\n            0  9 11\r\n            0  0 12\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003ey(:,:,3) = 13 14 16\r\n            0 15 17\r\n            0  0 18\r\n\u003c/pre\u003e\u003cp\u003e\u003ci\u003eNOTE:\u003c/i\u003e If you are wondering why this seems like a strange task, it is inspired by a genotype-\u0026gt;phenotype mapping I am doing in a genetic algorithm.\u003c/p\u003e","function_template":"function y = mat2triu3(x)\r\n  y = x;\r\nend","test_suite":"%%\r\nx = 1:100;\r\ny_correct = shiftdim(x,-1);\r\nassert(isequal(mat2triu3(x),y_correct))\r\n\r\n%%\r\nx = reshape(1:15,3,[]);\r\ny_correct(:,:,1) = [1 2;0 3];\r\ny_correct(:,:,2) = [4 5;0 6];\r\ny_correct(:,:,3) = [7 8;0 9];\r\ny_correct(:,:,4) = [10 11;0 12];\r\ny_correct(:,:,5) = [13 14;0 15];\r\nassert(isequal(mat2triu3(x),y_correct))\r\n\r\n%%\r\nx = reshape(1:18,3,[])';\r\ny_correct(:,:,1) = [1 4 10; 0 7 13; 0 0 16];\r\ny_correct(:,:,2) = [2 5 11; 0 8 14; 0 0 17];\r\ny_correct(:,:,3) = [3 6 12; 0 9 15; 0 0 18];\r\nassert(isequal(mat2triu3(x),y_correct))\r\n\r\n%%\r\nx = randi(50,sum(1:100),22);\r\ny = mat2triu3(x);\r\nmask = (y~=0);\r\nxb = reshape(y(mask),[],size(y,3));\r\nassert(isequal(size(y),[100 100 22]))\r\nassert(isequal(x,xb))\r\n\r\n","published":true,"deleted":false,"likes_count":3,"comments_count":0,"created_by":4793,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":135,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":23,"created_at":"2013-11-07T22:49:47.000Z","updated_at":"2026-03-31T09:10:36.000Z","published_at":"2013-11-07T22:58:51.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 2D numeric array x in which each column represents the vectorized form of an upper triangular matrix, return a 3D array y containing the concatenated triangular matrices.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eIf the size of the input matrix x is MxN, then the size of the output matrix y is PxPxN, where M = sum(1:P)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYou may assume that P\u0026lt;=100\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\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\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 = 1  7 13\\n    2  8 14\\n    3  9 15\\n    4 10 16\\n    5 11 17\\n    6 12 18]]\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\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[y(:,:,1) =  1  2  4\\n            0  3  5\\n            0  0  6\\n\\ny(:,:,2) =  7  8 10\\n            0  9 11\\n            0  0 12\\n\\ny(:,:,3) = 13 14 16\\n            0 15 17\\n            0  0 18]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eNOTE:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e If you are wondering why this seems like a strange task, it is inspired by a genotype-\u0026gt;phenotype mapping I am doing in a genetic algorithm.\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":939,"title":"DNA Pattern Match:  Performance  Metric - Speed","description":"The Challenge is to Rapidly find matches of DNA sequences, Length=6, in a 1,800,000 long DNA file.\r\n\r\nAt \u003chttp://imacst.com/issues/volume-3issue-1/ IMACST\u003e the paper \u003chttp://imacst.com/web_documents/1202002.pdf An Intelligent and Efficient Matching Algorithm to Finding a DNA Pattern\u003e claimed an astounding time improvement from  9.94 seconds to 7.84 seconds, 21% time reduction, to match six segments of length 6 in a 1.8M long DNA file. Basic probability asserts 1.8M/4^6 * 6 = 2637 matches. The paper's test case produced 2346 matches. The method employed used text processing in C++. The paper's L=25 and L=50 cases will be later challenges.\r\n\r\nMatlab can achieve matching a six pattern set of L=6 in \u003c15 msec (i5/16GB). This is merely a 99.8% time reduction.\r\n\r\n\r\n*Challenge Description:*\r\nDNA is made of letters ACGT, \u003chttp://en.wikipedia.org/wiki/DNA wiki DNA\u003e, which for the purposes of this Matlab Cody Challenge are given values 0 thru 3. (ACGT= 0123)\r\n\r\n*Input:* [DNA, DNA_ID, Patterns]\r\n\r\n* DNA is a 1.8M long uint8 row vector of values 0 thru 3.\r\n* DNA_ID identifies the DNA segment being processed. Multiple calls using the same DNA_ID will be performed. The first call of a DNA_ID is not timed.\r\n* Patterns is an Nx6 uint8 array where each row corresponds to a search pattern.\r\n\r\n*Output:* Locations\r\n\r\nLocations of all start indices that match any of the patterns\r\n\r\n*Scoring:* Average Time (msec) for a block of L=6 patterns\r\n\r\nExample:\r\n\r\n* DNA= [0 1 2 3 3 2 1 0 1 2 3 3]\r\n* Pattern = [2 3 3 2 1 0]\r\n* Locations = [3]\r\n\r\n*Hints:*\r\n\r\n* Vectorization (Base 4 to Base 10 of 6 character words)\r\n* Bitshift/Reshape to create all words\r\n* Logical Indexing\r\n \r\n\r\nComing soon: Genome DNA sequencing of PhagePhix174 and Haempphilus Influenza","description_html":"\u003cp\u003eThe Challenge is to Rapidly find matches of DNA sequences, Length=6, in a 1,800,000 long DNA file.\u003c/p\u003e\u003cp\u003eAt \u003ca href=\"http://imacst.com/issues/volume-3issue-1/\"\u003eIMACST\u003c/a\u003e the paper \u003ca href=\"http://imacst.com/web_documents/1202002.pdf\"\u003eAn Intelligent and Efficient Matching Algorithm to Finding a DNA Pattern\u003c/a\u003e claimed an astounding time improvement from  9.94 seconds to 7.84 seconds, 21% time reduction, to match six segments of length 6 in a 1.8M long DNA file. Basic probability asserts 1.8M/4^6 * 6 = 2637 matches. The paper's test case produced 2346 matches. The method employed used text processing in C++. The paper's L=25 and L=50 cases will be later challenges.\u003c/p\u003e\u003cp\u003eMatlab can achieve matching a six pattern set of L=6 in \u0026lt;15 msec (i5/16GB). This is merely a 99.8% time reduction.\u003c/p\u003e\u003cp\u003e\u003cb\u003eChallenge Description:\u003c/b\u003e\r\nDNA is made of letters ACGT, \u003ca href=\"http://en.wikipedia.org/wiki/DNA\"\u003ewiki DNA\u003c/a\u003e, which for the purposes of this Matlab Cody Challenge are given values 0 thru 3. (ACGT= 0123)\u003c/p\u003e\u003cp\u003e\u003cb\u003eInput:\u003c/b\u003e [DNA, DNA_ID, Patterns]\u003c/p\u003e\u003cul\u003e\u003cli\u003eDNA is a 1.8M long uint8 row vector of values 0 thru 3.\u003c/li\u003e\u003cli\u003eDNA_ID identifies the DNA segment being processed. Multiple calls using the same DNA_ID will be performed. The first call of a DNA_ID is not timed.\u003c/li\u003e\u003cli\u003ePatterns is an Nx6 uint8 array where each row corresponds to a search pattern.\u003c/li\u003e\u003c/ul\u003e\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e Locations\u003c/p\u003e\u003cp\u003eLocations of all start indices that match any of the patterns\u003c/p\u003e\u003cp\u003e\u003cb\u003eScoring:\u003c/b\u003e Average Time (msec) for a block of L=6 patterns\u003c/p\u003e\u003cp\u003eExample:\u003c/p\u003e\u003cul\u003e\u003cli\u003eDNA= [0 1 2 3 3 2 1 0 1 2 3 3]\u003c/li\u003e\u003cli\u003ePattern = [2 3 3 2 1 0]\u003c/li\u003e\u003cli\u003eLocations = [3]\u003c/li\u003e\u003c/ul\u003e\u003cp\u003e\u003cb\u003eHints:\u003c/b\u003e\u003c/p\u003e\u003cul\u003e\u003cli\u003eVectorization (Base 4 to Base 10 of 6 character words)\u003c/li\u003e\u003cli\u003eBitshift/Reshape to create all words\u003c/li\u003e\u003cli\u003eLogical Indexing\u003c/li\u003e\u003c/ul\u003e\u003cp\u003eComing soon: Genome DNA sequencing of PhagePhix174 and Haempphilus Influenza\u003c/p\u003e","function_template":"function Locations = find_DNA(DNA, DNA_ID, Patterns)\r\n persistent DNA_ID_loaded  % add other DNA created variables\r\n Locations=[];\r\n if isempty(DNA_ID_loaded) || DNA_ID~=DNA_ID_loaded\r\n  % Perform initialization actions on DNA vector - optional\r\n  DNA_ID_loaded=DNA_ID;\r\n  \r\n end % DNA_ID Processing\r\n\r\n\r\n\r\n \r\nend\r\n\r\n\r\n\r\n\r\n","test_suite":"%%\r\nfeval(@assignin,'caller','score',0);\r\n%%\r\n% Functionality Section\r\nPass=1;\r\nw=6;\r\nDNA=randi(4,1,1800000,'uint8')-1;\r\nPatterns=randi(4,12,6,'uint8')-1; % Create more than needed\r\nPatterns=unique(Patterns,'rows','stable'); % Elim dupes but maintain random\r\nPatterns=Patterns(1:6,:); % Take first 6 random vectors\r\nPatterns=unique(Patterns,'rows'); % Place in order for easier debugging\r\n\r\nDNA_ID=1;\r\nLocations = find_DNA(DNA, DNA_ID, Patterns);\r\nif isempty(Locations),Pass=0;end\r\nif max(size(Locations))\u003c1024,Pass=0;end\r\nfor i=1:size(Locations,1)\r\n      Pass=Pass \u0026\u0026 ismember(DNA(Locations(i,end):Locations(i,end)+w-1),Patterns,'rows');\r\n    end\r\n\r\nDNA=randi(4,1,1800000,'uint8')-1;\r\nPatterns=randi(4,24,6,'uint8')-1; % Create more than needed\r\nPatterns=unique(Patterns,'rows','stable'); % Elim dupes but maintain random\r\nPatterns=Patterns(1:12,:); % Take first 6 random vectors\r\nPatterns=unique(Patterns,'rows'); % Place in order for easier debugging\r\n\r\nDNA_ID=2;\r\nLocations = find_DNA(DNA, DNA_ID, Patterns);\r\nif isempty(Locations),Pass=0;end\r\nif max(size(Locations))\u003c1024,Pass=0;end\r\nfor i=1:size(Locations,1)\r\n      Pass=Pass \u0026\u0026 ismember(DNA(Locations(i,end):Locations(i,end)+w-1),Patterns,'rows');\r\n    end\r\n\r\n\r\nassert(isequal(Pass,1))\r\n%%\r\n% Timing Section\r\nDNA=randi(4,1,1800000,'uint8')-1;\r\nPatterns=randi(4,12,6,'uint8')-1; % Create more than needed\r\nPatterns=unique(Patterns,'rows','stable'); % Elim dupes but maintain random\r\nPatterns=Patterns(1:6,:); % Take first 6 random vectors\r\nPatterns=unique(Patterns,'rows'); % Place in order for easier debugging\r\n\r\nDNA_ID=1;\r\nLocations = find_DNA(DNA, DNA_ID, Patterns);\r\nLocations = find_DNA(DNA, DNA_ID, Patterns);\r\n\r\nt0=clock;\r\nfor i=1:10\r\n Locations = find_DNA(DNA, DNA_ID, Patterns);\r\nend\r\ndt1=etime(clock,t0)*1000; % msec conversion\r\nfprintf('Your Set 1 Time = %i msec\\n',floor(dt1))\r\n\r\n\r\nPatterns=randi(4,24,6,'uint8')-1; % Create more than needed\r\nPatterns=unique(Patterns,'rows','stable'); % Elim dupes but maintain random\r\nPatterns=Patterns(1:12,:); % Take first 12 random vectors\r\nPatterns=unique(Patterns,'rows'); % Place in order for easier debugging\r\n\r\nt0=clock;\r\nfor i=1:10\r\n Locations = find_DNA(DNA, DNA_ID, Patterns);\r\nend\r\ndt2=etime(clock,t0)*1000; % msec conversion\r\nfprintf('Your Set 2 Time = %i msec\\n',floor(dt2))\r\n\r\n\r\n% New DNA Set\r\nDNA=randi(4,1,1800000,'uint8')-1;\r\nPatterns=randi(4,12,6,'uint8')-1; % Create more than needed\r\nPatterns=unique(Patterns,'rows','stable'); % Elim dupes but maintain random\r\nPatterns=Patterns(1:6,:); % Take first 6 random vectors\r\nPatterns=unique(Patterns,'rows'); % Place in order for easier debugging\r\n\r\nDNA_ID=2;\r\nLocations = find_DNA(DNA, DNA_ID, Patterns);\r\nLocations = find_DNA(DNA, DNA_ID, Patterns);\r\n\r\nt0=clock;\r\nfor i=1:10\r\n Locations = find_DNA(DNA, DNA_ID, Patterns);\r\nend\r\ndt3=etime(clock,t0)*1000; % msec conversion\r\nfprintf('Your Set 3 Time = %i msec\\n',floor(dt3))\r\n\r\n\r\nfeval(@assignin,'caller','score',min(400,floor(double(dt1+dt2+dt3)/30)));\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":0,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":9,"test_suite_updated_at":"2012-09-10T03:41:37.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2012-09-10T00:09:00.000Z","updated_at":"2025-11-21T09:02:33.000Z","published_at":"2012-09-10T03:40:23.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe Challenge is to Rapidly find matches of DNA sequences, Length=6, in a 1,800,000 long DNA file.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eAt\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://imacst.com/issues/volume-3issue-1/\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eIMACST\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e the paper\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://imacst.com/web_documents/1202002.pdf\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eAn Intelligent and Efficient Matching Algorithm to Finding a DNA Pattern\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e claimed an astounding time improvement from 9.94 seconds to 7.84 seconds, 21% time reduction, to match six segments of length 6 in a 1.8M long DNA file. Basic probability asserts 1.8M/4^6 * 6 = 2637 matches. The paper's test case produced 2346 matches. The method employed used text processing in C++. The paper's L=25 and L=50 cases will be later challenges.\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\u003eMatlab can achieve matching a six pattern set of L=6 in \u0026lt;15 msec (i5/16GB). This is merely a 99.8% time reduction.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eChallenge Description:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e DNA is made of letters ACGT,\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://en.wikipedia.org/wiki/DNA\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ewiki DNA\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e, which for the purposes of this Matlab Cody Challenge are given values 0 thru 3. (ACGT= 0123)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eInput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e [DNA, DNA_ID, Patterns]\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eDNA is a 1.8M long uint8 row vector of values 0 thru 3.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eDNA_ID identifies the DNA segment being processed. Multiple calls using the same DNA_ID will be performed. The first call of a DNA_ID is not timed.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ePatterns is an Nx6 uint8 array where each row corresponds to a search pattern.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eOutput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Locations\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\u003eLocations of all start indices that match any of the patterns\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eScoring:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Average Time (msec) for a block of L=6 patterns\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=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eDNA= [0 1 2 3 3 2 1 0 1 2 3 3]\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ePattern = [2 3 3 2 1 0]\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eLocations = [3]\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eHints:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eVectorization (Base 4 to Base 10 of 6 character words)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eBitshift/Reshape to create all words\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eLogical Indexing\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\u003eComing soon: Genome DNA sequencing of PhagePhix174 and Haempphilus Influenza\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"}],"term":"tag:\"logical indexing\"","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:\"logical indexing\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"logical indexing\"","","\"","logical indexing","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007f19435c8b78\u003e":null,"#\u003cMathWorks::Search::Field:0x00007f19435c8ad8\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007f19435c8218\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007f19435c8df8\u003e":1,"#\u003cMathWorks::Search::Field:0x00007f19435c8d58\u003e":50,"#\u003cMathWorks::Search::Field:0x00007f19435c8cb8\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007f19435c8c18\u003e":"tag:\"logical indexing\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007f19435c8c18\u003e":"tag:\"logical indexing\""},"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:\"logical indexing\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"logical indexing\"","","\"","logical indexing","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007f19435c8b78\u003e":null,"#\u003cMathWorks::Search::Field:0x00007f19435c8ad8\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007f19435c8218\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007f19435c8df8\u003e":1,"#\u003cMathWorks::Search::Field:0x00007f19435c8d58\u003e":50,"#\u003cMathWorks::Search::Field:0x00007f19435c8cb8\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007f19435c8c18\u003e":"tag:\"logical indexing\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007f19435c8c18\u003e":"tag:\"logical indexing\""},"queried_facets":{}},"options":{"fields":["id","difficulty_rating"]},"join":" "},"results":[{"id":43607,"difficulty_rating":"easy"},{"id":42515,"difficulty_rating":"easy"},{"id":44450,"difficulty_rating":"easy"},{"id":694,"difficulty_rating":"easy"},{"id":640,"difficulty_rating":"easy-medium"},{"id":868,"difficulty_rating":"easy-medium"},{"id":320,"difficulty_rating":"easy-medium"},{"id":824,"difficulty_rating":"easy-medium"},{"id":1704,"difficulty_rating":"easy-medium"},{"id":856,"difficulty_rating":"easy-medium"},{"id":1972,"difficulty_rating":"medium"},{"id":939,"difficulty_rating":"medium"}]}}