{"group":{"id":1,"name":"Community","lockable":false,"created_at":"2012-01-18T18:02:15.000Z","updated_at":"2026-04-06T14:01:22.000Z","description":"Problems submitted by members of the MATLAB Central community.","is_default":true,"created_by":161519,"badge_id":null,"featured":false,"trending":false,"solution_count_in_trending_period":0,"trending_last_calculated":"2026-04-06T00:00:00.000Z","image_id":null,"published":true,"community_created":false,"status_id":2,"is_default_group_for_player":false,"deleted_by":null,"deleted_at":null,"restored_by":null,"restored_at":null,"description_opc":null,"description_html":null,"published_at":null},"problems":[{"id":42447,"title":"Define the operators of  function_handles","description":"Suppose f and g are function_handles, try to define f+g,f-g,f*g and f/g.\r\n     \r\n  e.g. \r\n     if   f = @(x)x and g = @(x)x+1\r\n     then\r\n          f+g = @(x)2*x+1\r\n          f-g = @(x)-1\r\n          f*g = @(x)x*(x+1)\r\n          f/g = @(x)x/(x+1)","description_html":"\u003cp\u003eSuppose f and g are function_handles, try to define f+g,f-g,f*g and f/g.\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003ee.g. \r\n   if   f = @(x)x and g = @(x)x+1\r\n   then\r\n        f+g = @(x)2*x+1\r\n        f-g = @(x)-1\r\n        f*g = @(x)x*(x+1)\r\n        f/g = @(x)x/(x+1)\r\n\u003c/pre\u003e","function_template":"function y = fho(f,g,type)\r\n  % 'type' define which operation to be performed\r\n  % for simplicity, 1 represents '+', 2 represents '-', 3 represents '*', 4 represents '/'.\r\n  y = x;\r\nend","test_suite":"%%\r\nf = @(x)x;\r\ng = @(x)x+1;\r\ny = fho(f,g,1)\r\ny_correct = 3;\r\nassert(isequal(y(1),y_correct))\r\n\r\n%%\r\nf = @sqrt;\r\ng = @ceil;\r\ny = fho(f,g,2)\r\ny_correct = -2;\r\nassert(isequal(y(4),y_correct))\r\n\r\n%%\r\nf = @nextpow2;\r\ng = @log10;\r\ny = fho(f,g,3)\r\ny_correct = 4;\r\nassert(isequal(y(10),y_correct))\r\n\r\n%%\r\nf = @floor;\r\ng = @ceil;\r\ny = fho(f,g,4)\r\ny_correct = 0.5;\r\nassert(isequal(y(1.25),y_correct))","published":true,"deleted":false,"likes_count":0,"comments_count":0,"created_by":40597,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":58,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2015-07-05T14:27:43.000Z","updated_at":"2026-03-31T11:28:04.000Z","published_at":"2015-07-05T15:08:46.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\u003eSuppose f and g are function_handles, try to define f+g,f-g,f*g and f/g.\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[e.g. \\n   if   f = @(x)x and g = @(x)x+1\\n   then\\n        f+g = @(x)2*x+1\\n        f-g = @(x)-1\\n        f*g = @(x)x*(x+1)\\n        f/g = @(x)x/(x+1)]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":581,"title":"Function composition","description":"Write a function that accepts two function handles f and g and returns the composition h. That is,\r\n\r\nh = (f o g)(x) = f(g(x))\r\n\r\nExample:\r\n\r\n  \u003e\u003e f = @(x)x^2;\r\n  \u003e\u003e g = @(x)x+1;\r\n  \u003e\u003e h = composeFcn(f,g);\r\n  \u003e\u003e h(3)\r\n  ans =\r\n    16\r\n    \r\nbecause (3+1)^2 = 16.","description_html":"\u003cp\u003eWrite a function that accepts two function handles f and g and returns the composition h. That is,\u003c/p\u003e\u003cp\u003eh = (f o g)(x) = f(g(x))\u003c/p\u003e\u003cp\u003eExample:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e\u0026gt;\u0026gt; f = @(x)x^2;\r\n\u0026gt;\u0026gt; g = @(x)x+1;\r\n\u0026gt;\u0026gt; h = composeFcn(f,g);\r\n\u0026gt;\u0026gt; h(3)\r\nans =\r\n  16\r\n\u003c/pre\u003e\u003cp\u003ebecause (3+1)^2 = 16.\u003c/p\u003e","function_template":"function h = composeFcn(f,g)\r\nend","test_suite":"%%\r\nf = @(x)x^2;\r\ng = @(x)x+1;\r\nh = composeFcn(f,g);\r\nassert(isequal(h(3),16));\r\n\r\n%%\r\nf = @round;\r\ng = @sqrt;\r\nh = composeFcn(f,g);\r\nassert(isequal(h(8),3));","published":true,"deleted":false,"likes_count":1,"comments_count":8,"created_by":19,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":261,"test_suite_updated_at":"2017-04-26T13:06:48.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2012-04-13T18:57:19.000Z","updated_at":"2026-04-02T10:42:01.000Z","published_at":"2012-04-13T19:27:06.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eWrite a function that accepts two function handles f and g and returns the composition h. That is,\u003c/w:t\u003e\u003c/w:r\u003e\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\u003eh = (f o g)(x) = f(g(x))\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExample:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[\u003e\u003e f = @(x)x^2;\\n\u003e\u003e g = @(x)x+1;\\n\u003e\u003e h = composeFcn(f,g);\\n\u003e\u003e h(3)\\nans =\\n  16]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ebecause (3+1)^2 = 16.\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":44083,"title":"First use of arrayfun() and anonymous function @(x)","description":"Create an anonymous function using @(x) for a parabola equation for the given coefficients stored in s with\r\ns(1)x2 + s(2)x + s(3).\r\nUse arrayfun() to apply the parabola equation to each element in the array A.\r\nNote: for , while , and eval are forbidden.\r\nHave fun :o)","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: 141px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 407px 70.5px; transform-origin: 407px 70.5px; vertical-align: baseline; \"\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 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: 337px 8px; transform-origin: 337px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eCreate an anonymous function using @(x) for a parabola equation for the given coefficients stored in s with\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: 65.5px 8px; transform-origin: 65.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003es(1)x2 + s(2)x + s(3).\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: 243px 8px; transform-origin: 243px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eUse arrayfun() to apply the parabola equation to each element in the array A.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 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: 16.5px 8px; transform-origin: 16.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eNote:\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 2px 8px; transform-origin: 2px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 8.5px 8px; transform-origin: 8.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"font-style: italic; \"\u003efor\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 4px 8px; transform-origin: 4px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e ,\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 2px 8px; transform-origin: 2px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 15.5px 8px; transform-origin: 15.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"font-style: italic; \"\u003ewhile\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 18px 8px; transform-origin: 18px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e , and\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 2px 8px; transform-origin: 2px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 12.5px 8px; transform-origin: 12.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"font-style: italic; \"\u003eeval\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 46.5px 8px; transform-origin: 46.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e are forbidden.\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: 38.5px 8px; transform-origin: 38.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eHave fun :o)\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function B = parabola_equation(A,s)\r\n    \r\n  B = A;\r\n  \r\nend","test_suite":"%%\r\nA = [0 1 2];\r\ns = [1 2 3];\r\ny_correct = [3 6 11];\r\nassert(isequal(parabola_equation(A,s),y_correct))\r\n%%\r\nA = -12:4:11;\r\ns = [-5 -8 0];\r\ny_correct = [-624  -256   -48     0  -112  -384];\r\nassert(isequal(parabola_equation(A,s),y_correct))\r\n%%\r\nA = -2:2;\r\ns = [0 pi 0];\r\ny_correct = [-2*pi -pi 0  pi 2*pi];\r\nassert(isequal(parabola_equation(A,s),y_correct))\r\n%%\r\nassert(isempty(regexp(evalc('type parabola_equation'),'(eval|for|while|polyval|)')))\r\nassert(not(isempty(regexp(evalc('type parabola_equation'),'(@)'))))\r\nassert(not(isempty(regexp(evalc('type parabola_equation'),'arrayfun'))))\r\n","published":true,"deleted":false,"likes_count":0,"comments_count":1,"created_by":100084,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":72,"test_suite_updated_at":"2022-03-01T19:46:58.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2017-03-10T22:47:36.000Z","updated_at":"2026-03-16T13:46:36.000Z","published_at":"2017-03-10T22:48:28.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eCreate an anonymous function using @(x) for a parabola equation for the given coefficients stored in s with\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\u003es(1)x2 + s(2)x + s(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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eUse arrayfun() to apply the parabola equation to each element in the array A.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eNote:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003efor\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: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\u003ewhile\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:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eeval\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e are forbidden.\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\u003eHave fun :o)\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":260,"title":"Create a function handle that reverses the input arguments of another function handle","description":"Given a function that takes two input arguments and returns one output, create another function handle that performs the same operation but with the input arguments in reverse order.\r\n\r\nFor example:\r\n\r\n   f = @(x,y) 2*x+y;\r\n   f(5,6)\r\n\r\nreturns the answer 16. Your function should produce another function handle that performs the same operation except that y is the first input argument and x is the second.\r\n\r\n   g = reverseArguments(f);\r\n   g(6,5)\r\n\r\nreturns 16, and:\r\n\r\n   g(5,6)\r\n\r\nreturns 17.\r\n","description_html":"\u003cp\u003eGiven a function that takes two input arguments and returns one output, create another function handle that performs the same operation but with the input arguments in reverse order.\u003c/p\u003e\u003cp\u003eFor example:\u003c/p\u003e\u003cpre\u003e   f = @(x,y) 2*x+y;\r\n   f(5,6)\u003c/pre\u003e\u003cp\u003ereturns the answer 16. Your function should produce another function handle that performs the same operation except that y is the first input argument and x is the second.\u003c/p\u003e\u003cpre\u003e   g = reverseArguments(f);\r\n   g(6,5)\u003c/pre\u003e\u003cp\u003ereturns 16, and:\u003c/p\u003e\u003cpre\u003e   g(5,6)\u003c/pre\u003e\u003cp\u003ereturns 17.\u003c/p\u003e","function_template":"function g = reverseArguments(f)\r\n  g = @sin;\r\nend","test_suite":"%%\r\nf = @(x,y) 2*x+y;\r\ng = reverseArguments(f);\r\nassert(isequal(g(5,6), 17));\r\nassert(isequal(g(6,5), 16));\r\n\r\n%%\r\nf = @(x,y) x.^y;\r\ng = reverseArguments(f);\r\nassert(isequal(g(2,3), 9));\r\n\r\n%%\r\nf = @(A,theta) A*sin(theta);\r\ng = reverseArguments(f);\r\nassert(isequal(g(4,10), 10*sin(4)));\r\n","published":true,"deleted":false,"likes_count":3,"comments_count":0,"created_by":4303371,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":157,"test_suite_updated_at":"2012-02-05T02:33:43.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2012-02-05T02:33:43.000Z","updated_at":"2025-12-15T21:13:28.000Z","published_at":"2012-02-05T02:34:46.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 function that takes two input arguments and returns one output, create another function handle that performs the same operation but with the input arguments in reverse order.\u003c/w:t\u003e\u003c/w:r\u003e\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[   f = @(x,y) 2*x+y;\\n   f(5,6)]]\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 answer 16. Your function should produce another function handle that performs the same operation except that y is the first input argument and x is the second.\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[   g = reverseArguments(f);\\n   g(6,5)]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ereturns 16, and:\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[   g(5,6)]]\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 17.\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":1196,"title":"Apply a function array to an array of numbers ","description":"It is required to apply a cell array of functions to a numerical array, where the functions accept only scalar inputs.\r\n\r\nExample:\r\n\r\n\u003e\u003e f{1} = @(x) x;\r\n\r\n\u003e\u003e f{2} = @(x) x^2;\r\n\r\n\u003e\u003e f{3} = @(x) x^3;\r\n\r\n\u003e\u003e x = 1:5;\r\n\r\n\u003e\u003e arfn(f,x)\r\n\r\nans =\r\n\r\n     1     2     3     4     5\r\n     1     4     9    16    25\r\n     1     8    27    64   125","description_html":"\u003cp\u003eIt is required to apply a cell array of functions to a numerical array, where the functions accept only scalar inputs.\u003c/p\u003e\u003cp\u003eExample:\u003c/p\u003e\u003cp\u003e\u003e\u003e f{1} = @(x) x;\u003c/p\u003e\u003cp\u003e\u003e\u003e f{2} = @(x) x^2;\u003c/p\u003e\u003cp\u003e\u003e\u003e f{3} = @(x) x^3;\u003c/p\u003e\u003cp\u003e\u003e\u003e x = 1:5;\u003c/p\u003e\u003cp\u003e\u003e\u003e arfn(f,x)\u003c/p\u003e\u003cp\u003eans =\u003c/p\u003e\u003cpre\u003e     1     2     3     4     5\r\n     1     4     9    16    25\r\n     1     8    27    64   125\u003c/pre\u003e","function_template":"function y = arfn(f,x)\r\n  y = x;\r\nend","test_suite":"%%\r\nf{1} = @(x) x;\r\nf{2} = @(x) x^2;\r\nf{3} = @(x) x^3;\r\nx = 1:5;\r\ny_correct = [     1     2     3     4     5\r\n                  1     4     9    16    25\r\n                  1     8    27    64   125];\r\nassert(isequal(arfn(f,x),y_correct))\r\n\r\n%%\r\nf{1} = @(x) 1/x;\r\nf{2} = @(x) x^0.5;\r\nf{3} = @(x) x-4;\r\nx = [4 16 25 100];\r\ny_correct = [0.25    0.0625    0.04    0.01\r\n             2       4         5       10\r\n             0       12        21      96];\r\nassert(isequal(arfn(f,x),y_correct))\r\n","published":true,"deleted":false,"likes_count":2,"comments_count":0,"created_by":3399,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":102,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2013-01-11T11:58:13.000Z","updated_at":"2025-12-19T04:32:02.000Z","published_at":"2013-01-11T12:08:22.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eIt is required to apply a cell array of functions to a numerical array, where the functions accept only scalar inputs.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExample:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u0026gt;\u0026gt; f{1} = @(x) x;\u003c/w:t\u003e\u003c/w:r\u003e\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\u0026gt;\u0026gt; f{2} = @(x) x^2;\u003c/w:t\u003e\u003c/w:r\u003e\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\u0026gt;\u0026gt; f{3} = @(x) x^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\u003e\u0026gt;\u0026gt; x = 1: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\u003e\u0026gt;\u0026gt; arfn(f,x)\u003c/w:t\u003e\u003c/w:r\u003e\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\u003eans =\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[     1     2     3     4     5\\n     1     4     9    16    25\\n     1     8    27    64   125]]\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":429,"title":"function on a moving window","description":"Create a function that applies an operation (such as @sum, @mean, @std, @norm etc) to a moving window of the data.\r\nFirst example:\r\n    filtered = mopt(@mean,1:6,1,2)\r\n\r\n    Then filtered = [NaN 2.5000 3.5000 4.5000 NaN NaN]\r\nThis is the moving average.\r\nSecond example:\r\n    filtered = mopt(@std,[0.2 0.8 0.7 1.1 1.1 1.0 0.2],2,0)\r\n\r\n    Then filtered = [NaN NaN 0.3215 0.2082 0.2309 0.0577 0.4933]\r\nThis is the 'moving standard error'.\r\nThe first arg of mopt is a function handle. It must be a function that takes a vector as input and produces a scalar as output.\r\nThe second arg is the vector of data.\r\nThe third and fourth args are the lags and leads that determin the size of the moving window.","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: 396.6px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 407px 198.3px; transform-origin: 407px 198.3px; 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: 377px 8px; transform-origin: 377px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eCreate a function that applies an operation (such as @sum, @mean, @std, @norm etc) to a moving window of the data.\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: 44px 8px; transform-origin: 44px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eFirst example:\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgb(247, 247, 247); block-size: 61.3px; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-end-end-radius: 4px; border-end-start-radius: 4px; border-start-end-radius: 4px; border-start-start-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; margin-block-end: 10px; margin-block-start: 10px; margin-bottom: 10px; margin-inline-end: 3px; margin-inline-start: 3px; margin-left: 3px; margin-right: 3px; margin-top: 10px; perspective-origin: 404px 30.65px; transform-origin: 404px 30.65px; margin-left: 3px; margin-top: 10px; margin-bottom: 10px; margin-right: 3px; \"\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 136px 8.5px; tab-size: 4; transform-origin: 136px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e    filtered = mopt(@mean,1:6,1,2)\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 0px 8.5px; tab-size: 4; transform-origin: 0px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 216px 8.5px; tab-size: 4; transform-origin: 216px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; perspective-origin: 36px 8.5px; transform-origin: 36px 8.5px; \"\u003e    Then \u003c/span\u003e\u003cspan style=\"border-block-end-color: rgb(170, 4, 249); border-block-start-color: rgb(170, 4, 249); border-bottom-color: rgb(170, 4, 249); border-inline-end-color: rgb(170, 4, 249); border-inline-start-color: rgb(170, 4, 249); border-left-color: rgb(170, 4, 249); border-right-color: rgb(170, 4, 249); border-top-color: rgb(170, 4, 249); caret-color: rgb(170, 4, 249); color: rgb(170, 4, 249); column-rule-color: rgb(170, 4, 249); margin-inline-end: 0px; margin-right: 0px; outline-color: rgb(170, 4, 249); perspective-origin: 180px 8.5px; text-decoration-color: rgb(170, 4, 249); text-emphasis-color: rgb(170, 4, 249); transform-origin: 180px 8.5px; \"\u003efiltered = [NaN 2.5000 3.5000 4.5000 NaN NaN]\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 10px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 10px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 10px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 87px 8px; transform-origin: 87px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eThis is the moving average.\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: 54px 8px; transform-origin: 54px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eSecond example:\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgb(247, 247, 247); block-size: 61.3px; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-end-end-radius: 4px; border-end-start-radius: 4px; border-start-end-radius: 4px; border-start-start-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; margin-block-end: 10px; margin-block-start: 10px; margin-bottom: 10px; margin-inline-end: 3px; margin-inline-start: 3px; margin-left: 3px; margin-right: 3px; margin-top: 10px; perspective-origin: 404px 30.65px; transform-origin: 404px 30.65px; margin-left: 3px; margin-top: 10px; margin-bottom: 10px; margin-right: 3px; \"\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 236px 8.5px; tab-size: 4; transform-origin: 236px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e    filtered = mopt(@std,[0.2 0.8 0.7 1.1 1.1 1.0 0.2],2,0)\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 0px 8.5px; tab-size: 4; transform-origin: 0px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 256px 8.5px; tab-size: 4; transform-origin: 256px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; perspective-origin: 36px 8.5px; transform-origin: 36px 8.5px; \"\u003e    Then \u003c/span\u003e\u003cspan style=\"border-block-end-color: rgb(170, 4, 249); border-block-start-color: rgb(170, 4, 249); border-bottom-color: rgb(170, 4, 249); border-inline-end-color: rgb(170, 4, 249); border-inline-start-color: rgb(170, 4, 249); border-left-color: rgb(170, 4, 249); border-right-color: rgb(170, 4, 249); border-top-color: rgb(170, 4, 249); caret-color: rgb(170, 4, 249); color: rgb(170, 4, 249); column-rule-color: rgb(170, 4, 249); margin-inline-end: 0px; margin-right: 0px; outline-color: rgb(170, 4, 249); perspective-origin: 220px 8.5px; text-decoration-color: rgb(170, 4, 249); text-emphasis-color: rgb(170, 4, 249); transform-origin: 220px 8.5px; \"\u003efiltered = [NaN NaN 0.3215 0.2082 0.2309 0.0577 0.4933]\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 10px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 10px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 10px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 109.5px 8px; transform-origin: 109.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eThis is the 'moving standard error'.\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: 366.5px 8px; transform-origin: 366.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eThe first arg of mopt is a function handle. It must be a function that takes a vector as input and produces a scalar as output.\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: 116.5px 8px; transform-origin: 116.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eThe second arg is the vector of data.\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: 291.5px 8px; transform-origin: 291.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eThe third and fourth args are the lags and leads that determin the size of the moving window.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function filtered = mopt(func,data,lag,lead)\r\n  filtered = data;\r\nend","test_suite":"%%\r\nx = 1:6;\r\ny = mopt(@mean,x,1,2);\r\ny_correct = [NaN 2.5000 3.5000 4.5000 NaN NaN];\r\nassert(isequal(y(2:end-2),y_correct(2:end-2)) \u0026\u0026 all(isnan(y([1,5,6]))))\r\n\r\n%%\r\nx = [0.2 0.8 0.7 1.1 1.1 1.0 0.2];\r\ny = mopt(@std,x,2,0);\r\ny_correct = [NaN NaN 0.321455025366432 0.208166599946613 0.23094010767585 0.0577350269189626 0.493288286231625];\r\nassert(max(abs(y_correct(3:end)-y(3:end)))\u003c2*eps \u0026\u0026 all(isnan(y(1:2))))\r\n\r\n%%\r\nx = rand(1,10);\r\ny_correct = x;\r\ny = mopt(@mean,x,0,0);\r\nassert(isequal(y,y_correct))\r\n\r\n%%\r\nx = randi(100,1,randi(100));\r\ny_correct = [NaN movsum(x,3,'Endpoints','Discard') NaN];\r\ny = mopt(@sum,x,1,1);\r\nassert(isequaln(y,y_correct))\r\n","published":true,"deleted":false,"likes_count":2,"comments_count":1,"created_by":1939,"edited_by":223089,"edited_at":"2022-12-26T10:37:47.000Z","deleted_by":null,"deleted_at":null,"solvers_count":52,"test_suite_updated_at":"2022-12-26T10:37:47.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2012-02-29T22:04:47.000Z","updated_at":"2025-12-15T21:37:36.000Z","published_at":"2012-02-29T23:39:09.000Z","restored_at":null,"restored_by":null,"spam":null,"simulink":false,"admin_reviewed":false,"description_opc":"{\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eCreate a function that applies an operation (such as @sum, @mean, @std, @norm etc) to a moving window of the data.\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\u003eFirst 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[    filtered = mopt(@mean,1:6,1,2)\\n\\n    Then filtered = [NaN 2.5000 3.5000 4.5000 NaN NaN]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThis is the moving average.\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\u003eSecond 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[    filtered = mopt(@std,[0.2 0.8 0.7 1.1 1.1 1.0 0.2],2,0)\\n\\n    Then filtered = [NaN NaN 0.3215 0.2082 0.2309 0.0577 0.4933]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThis is the 'moving standard error'.\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\u003eThe first arg of mopt is a function handle. It must be a function that takes a vector as input and produces a scalar as output.\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\u003eThe second arg is the vector of data.\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\u003eThe third and fourth args are the lags and leads that determin the size of the moving window.\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":43685,"title":"Apply Function to Each Field of a Structure Array: Part 1","description":"The builtin \u003chttp://www.mathworks.com/help/matlab/ref/structfun.html?=structfun structfun\u003e applies a function handle to each field of a *scalar structure*, but it does not work for structure array. The purpose of this problem is to generalize structfun to enable support for structure array input.\r\n\r\nWrite your own function *structfun2* which accepts a structure array *s* and a function handle *f* as the inputs. The output *c = structfun2(f,s)* is a cell array of the same size as *s*, with each cell storing the result of *f* applied to every field of the relevant structure element in *s*. The structfun2 must preserve the behavior of structfun for scalar structure input. That is, structfun2(f,s) = structfun(f,s) holds for any scalar structure *s*. The assumption required is that *f* is a function that returns a scalar, regardless of the input. \r\n\r\nExample: \r\n\r\n  s = struct('f1',{1, [3 4]; 1, [5 6]},'f2',{[1 2], 2; [3 4 5], [2 5]});\r\n  f = @numel;\r\n  c = {[1;2], [2;1]; [1;3], [2;2]};\r\n\r\nRelated problems in this series:\r\n\r\n* \u003chttp://www.mathworks.com/matlabcentral/cody/problems/43686-apply-function-to-each-field-of-a-structure-array-part-2 Apply Function to Each Field of a Structure Array: Part 2\u003e\r\n* \u003chttp://www.mathworks.com/matlabcentral/cody/problems/43685-apply-function-to-each-field-of-a-structure-array-part-1 Apply Function to Each Field of a Structure Array: Part 1\u003e","description_html":"\u003cp\u003eThe builtin \u003ca href = \"http://www.mathworks.com/help/matlab/ref/structfun.html?=structfun\"\u003estructfun\u003c/a\u003e applies a function handle to each field of a \u003cb\u003escalar structure\u003c/b\u003e, but it does not work for structure array. The purpose of this problem is to generalize structfun to enable support for structure array input.\u003c/p\u003e\u003cp\u003eWrite your own function \u003cb\u003estructfun2\u003c/b\u003e which accepts a structure array \u003cb\u003es\u003c/b\u003e and a function handle \u003cb\u003ef\u003c/b\u003e as the inputs. The output \u003cb\u003ec = structfun2(f,s)\u003c/b\u003e is a cell array of the same size as \u003cb\u003es\u003c/b\u003e, with each cell storing the result of \u003cb\u003ef\u003c/b\u003e applied to every field of the relevant structure element in \u003cb\u003es\u003c/b\u003e. The structfun2 must preserve the behavior of structfun for scalar structure input. That is, structfun2(f,s) = structfun(f,s) holds for any scalar structure \u003cb\u003es\u003c/b\u003e. The assumption required is that \u003cb\u003ef\u003c/b\u003e is a function that returns a scalar, regardless of the input.\u003c/p\u003e\u003cp\u003eExample:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003es = struct('f1',{1, [3 4]; 1, [5 6]},'f2',{[1 2], 2; [3 4 5], [2 5]});\r\nf = @numel;\r\nc = {[1;2], [2;1]; [1;3], [2;2]};\r\n\u003c/pre\u003e\u003cp\u003eRelated problems in this series:\u003c/p\u003e\u003cul\u003e\u003cli\u003e\u003ca href = \"http://www.mathworks.com/matlabcentral/cody/problems/43686-apply-function-to-each-field-of-a-structure-array-part-2\"\u003eApply Function to Each Field of a Structure Array: Part 2\u003c/a\u003e\u003c/li\u003e\u003cli\u003e\u003ca href = \"http://www.mathworks.com/matlabcentral/cody/problems/43685-apply-function-to-each-field-of-a-structure-array-part-1\"\u003eApply Function to Each Field of a Structure Array: Part 1\u003c/a\u003e\u003c/li\u003e\u003c/ul\u003e","function_template":"function y = structfun2(f,s)\r\n  y = x;\r\nend","test_suite":"%%\r\ns.f1 = 'Sunday';\r\ns.f2 = 'Monday';\r\ns.f3 = 'Tuesday'; \r\ns.f4 = 'Wednesday';\r\ns.f5 = 'Thursday';\r\ns.f6 = 'Friday';\r\ns.f7 = 'Saturday';\r\nf = @numel;\r\nc = structfun(@numel, s);\r\nassert(isequal(structfun2(f,s),c))\r\n\r\n%%\r\ns = struct('f1',{1, [3 4]; 1, [5 6]},'f2',{[1 2], 2; [3 4 5], [2 5]});\r\nf = @(x)min(x(:));\r\nc = {[1;1], [3;2]; [1;3], [5;2]};\r\nassert(isequal(structfun2(f,s),c))\r\n\r\n%%\r\ns = struct('f1',{1, [3 4]; 1, [5 6]},'f2',{[1 2], 2; [3 4 5], [2 5]});\r\nf = @(x)max(x(:));\r\nc = {[1;2], [4;2]; [1;5], [6;5]};\r\nassert(isequal(structfun2(f,s),c))\r\n\r\n%%\r\ns = struct('f1',{0, [3 4]; 1, [5 7 6]},'f2',{[0 0], 2; [2 4 5], [0 5]});\r\nf = @nnz;\r\nc = {[0;0], [2;1]; [1;3], [3;1]};\r\nassert(isequal(structfun2(f,s),c))\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":0,"created_by":12569,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":31,"test_suite_updated_at":"2016-12-01T22:39:09.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2016-11-27T01:04:06.000Z","updated_at":"2025-12-14T11:39:11.000Z","published_at":"2016-11-27T01:32:39.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 builtin\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/help/matlab/ref/structfun.html?=structfun\\\"\u003e\u003cw:r\u003e\u003cw:t\u003estructfun\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e applies a function handle to each field of a\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003escalar structure\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, but it does not work for structure array. The purpose of this problem is to generalize structfun to enable support for structure array input.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eWrite your own function\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\u003estructfun2\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e which accepts a structure 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:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003es\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e and a function handle\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\u003ef\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e as the inputs. The output\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\u003ec = structfun2(f,s)\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e is a cell array of the same size as\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\u003es\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, with each cell storing the result of\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ef\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e applied to every field of the relevant structure element 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:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003es\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e. The structfun2 must preserve the behavior of structfun for scalar structure input. That is, structfun2(f,s) = structfun(f,s) holds for any scalar structure\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\u003es\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e. The assumption required is that\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\u003ef\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e is a function that returns a scalar, regardless of the input.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\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[s = struct('f1',{1, [3 4]; 1, [5 6]},'f2',{[1 2], 2; [3 4 5], [2 5]});\\nf = @numel;\\nc = {[1;2], [2;1]; [1;3], [2;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\u003eRelated problems in this series:\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:hyperlink w:docLocation=\\\"http://www.mathworks.com/matlabcentral/cody/problems/43686-apply-function-to-each-field-of-a-structure-array-part-2\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eApply Function to Each Field of a Structure Array: Part 2\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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:hyperlink w:docLocation=\\\"http://www.mathworks.com/matlabcentral/cody/problems/43685-apply-function-to-each-field-of-a-structure-array-part-1\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eApply Function to Each Field of a Structure Array: Part 1\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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":2641,"title":"Dispatch and collect ","description":"Write a function that dispatches the single argument _x_ to multiple function handles (varargin) and concatenates vertically the respective outputs of these functions. All the functions are guaranteed to return the same numbers of outputs of the same size.\r\n\r\nFor example, given\r\n\r\n  x = [1 2 6\r\n       2 7 5 \r\n       3 5 4];\r\n  [bounds, positions] = dispatch(x, @min, @max)\r\n\r\nbounds and position should be:\r\n\r\n  bounds = [1 2 4       %first output of min\r\n            3 7 6]      %first output of max\r\n  positions = [1 1 3    %second output of min \r\n               3 2 1]   %second output of max\r\n\r\n\r\n","description_html":"\u003cp\u003eWrite a function that dispatches the single argument \u003ci\u003ex\u003c/i\u003e to multiple function handles (varargin) and concatenates vertically the respective outputs of these functions. All the functions are guaranteed to return the same numbers of outputs of the same size.\u003c/p\u003e\u003cp\u003eFor example, given\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003ex = [1 2 6\r\n     2 7 5 \r\n     3 5 4];\r\n[bounds, positions] = dispatch(x, @min, @max)\r\n\u003c/pre\u003e\u003cp\u003ebounds and position should be:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003ebounds = [1 2 4       %first output of min\r\n          3 7 6]      %first output of max\r\npositions = [1 1 3    %second output of min \r\n             3 2 1]   %second output of max\r\n\u003c/pre\u003e","function_template":"function varargout = dispatch(x, varargin)\r\n  varargout{:} = [];\r\nend","test_suite":"%% 2 outputs, 2 functions\r\nx = [1 2 6; 2 7 5; 3 5 4];\r\nco1 = [1 2 4; 3 7 6];\r\nco2 = [1 1 3; 3 2 1];\r\n[o1, o2] = dispatch(x, @min, @max);\r\nassert(isequal(o1, co1) \u0026\u0026 isequal(o2, co2))\r\n\r\n%% 1 output, 3 functions\r\nx = randi(50, 20);\r\nco = [mean(x); mode(x); median(x)];\r\nassert(isequal(co, dispatch(x, @mean, @mode, @median)))\r\n\r\n%%  1 output, 5 functions\r\nx=10;\r\nco = [zeros(x);ones(x);eye(x);magic(x);pascal(x)];\r\nassert(isequal(co, dispatch(x, @zeros, @ones, @eye, @magic, @pascal)))\r\n\r\n%% 4 outputs, 1 function\r\nco = randi(50, 1, 4);\r\n[o1, o2, o3, o4] = dispatch(zeros(co), @size);\r\nassert(isequal([o1 o2 o3 o4], co))\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":0,"created_by":999,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":15,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2014-10-23T08:30:27.000Z","updated_at":"2025-09-22T07:23:48.000Z","published_at":"2014-10-23T10:00:26.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\u003eWrite a function that dispatches the single argument\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 to multiple function handles (varargin) and concatenates vertically the respective outputs of these functions. All the functions are guaranteed to return the same numbers of outputs of the same size.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eFor example, given\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 2 6\\n     2 7 5 \\n     3 5 4];\\n[bounds, positions] = dispatch(x, @min, @max)]]\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\u003ebounds and position should 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[bounds = [1 2 4       %first output of min\\n          3 7 6]      %first output of max\\npositions = [1 1 3    %second output of min \\n             3 2 1]   %second output of max]]\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":1210,"title":"Two-output anonymous function?","description":"Return a function handle that when applied to an input, it produces two outputs: the first is the same as the input, and the second is the element-wise square of the input.\r\n\r\nExample:\r\n\r\nf=f2f();\r\n[a b]=f([1 2 3])\r\n\r\na = \r\n1 2 3\r\n\r\nb =\r\n1 4 9\r\n\r\n","description_html":"\u003cp\u003eReturn a function handle that when applied to an input, it produces two outputs: the first is the same as the input, and the second is the element-wise square of the input.\u003c/p\u003e\u003cp\u003eExample:\u003c/p\u003e\u003cp\u003ef=f2f();\r\n[a b]=f([1 2 3])\u003c/p\u003e\u003cp\u003ea = \r\n1 2 3\u003c/p\u003e\u003cp\u003eb =\r\n1 4 9\u003c/p\u003e","function_template":"function y = f2f()\r\n  y = 1;\r\nend","test_suite":"%%\r\nx = [1 2 3];\r\na_correct = x;\r\nb_correct = x.^2;\r\nf=f2f();\r\n[a b]=f(x);\r\nassert(isequal(a,a_correct)\u0026\u0026isequal(b,b_correct))\r\n\r\n%%\r\nx = [-1 2 -3 5 -1];\r\na_correct = x;\r\nb_correct = x.^2;\r\nf=f2f();\r\n[a b]=f(x);\r\nassert(isequal(a,a_correct)\u0026\u0026isequal(b,b_correct))\r\n","published":true,"deleted":false,"likes_count":4,"comments_count":2,"created_by":3399,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":84,"test_suite_updated_at":null,"rescore_all_solutions":true,"group_id":1,"created_at":"2013-01-17T17:17:14.000Z","updated_at":"2025-12-10T11:16:02.000Z","published_at":"2013-01-17T17:19: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\",\"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\u003eReturn a function handle that when applied to an input, it produces two outputs: the first is the same as the input, and the second is the element-wise square of the input.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\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\u003ef=f2f(); [a b]=f([1 2 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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ea = 1 2 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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eb = 1 4 9\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":582,"title":"Function composition - harder","description":"Write a function that accepts an arbitrary number of function handles f_1, f_2, ..., f_n and returns the composition h. That is,\r\n\r\nh = (f_1 o f_2 o ... o f_n)(x) = f_1(f_2( ... f_n(x) ... ))\r\n\r\nExample:\r\n\r\n  \u003e\u003e f1 = @(x)x+1;\r\n  \u003e\u003e f2 = @(x)3*x;\r\n  \u003e\u003e f3 = @sqrt;\r\n  \u003e\u003e h = composeFcn(f1,f2,f3);\r\n  \u003e\u003e h(9)\r\n  ans =\r\n    10\r\n    \r\nbecause 3*sqrt(9)+1 = 10.\r\n\r\nYou can assume that there will always be at least one input passed to the *composeFcn* function.","description_html":"\u003cp\u003eWrite a function that accepts an arbitrary number of function handles f_1, f_2, ..., f_n and returns the composition h. That is,\u003c/p\u003e\u003cp\u003eh = (f_1 o f_2 o ... o f_n)(x) = f_1(f_2( ... f_n(x) ... ))\u003c/p\u003e\u003cp\u003eExample:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e\u0026gt;\u0026gt; f1 = @(x)x+1;\r\n\u0026gt;\u0026gt; f2 = @(x)3*x;\r\n\u0026gt;\u0026gt; f3 = @sqrt;\r\n\u0026gt;\u0026gt; h = composeFcn(f1,f2,f3);\r\n\u0026gt;\u0026gt; h(9)\r\nans =\r\n  10\r\n\u003c/pre\u003e\u003cp\u003ebecause 3*sqrt(9)+1 = 10.\u003c/p\u003e\u003cp\u003eYou can assume that there will always be at least one input passed to the \u003cb\u003ecomposeFcn\u003c/b\u003e function.\u003c/p\u003e","function_template":"function h = composeFcn(varargin)\r\nend","test_suite":"%%\r\nf1 = @(x)x+1;\r\nf2 = @(x)3*x;\r\nf3 = @sqrt;\r\nh = composeFcn(f1,f2,f3);\r\nassert(isequal(h(9),10));\r\n\r\n%%\r\nf = repmat({@(x)x+1},1,100);\r\nh = composeFcn(f{:});\r\nassert(isequal(h(0),100));\r\n\r\n%%\r\nf = @(x)x;\r\nh = composeFcn(f);\r\nassert(isequal(h(1234),1234));","published":true,"deleted":false,"likes_count":6,"comments_count":2,"created_by":19,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":106,"test_suite_updated_at":"2017-03-03T15:36:10.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2012-04-13T18:58:27.000Z","updated_at":"2025-12-05T11:00:12.000Z","published_at":"2012-04-13T19:27:40.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\u003eWrite a function that accepts an arbitrary number of function handles f_1, f_2, ..., f_n and returns the composition h. That is,\u003c/w:t\u003e\u003c/w:r\u003e\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\u003eh = (f_1 o f_2 o ... o f_n)(x) = f_1(f_2( ... f_n(x) ... ))\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExample:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[\u003e\u003e f1 = @(x)x+1;\\n\u003e\u003e f2 = @(x)3*x;\\n\u003e\u003e f3 = @sqrt;\\n\u003e\u003e h = composeFcn(f1,f2,f3);\\n\u003e\u003e h(9)\\nans =\\n  10]]\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\u003ebecause 3*sqrt(9)+1 = 10.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYou can assume that there will always be at least one input passed to the\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ecomposeFcn\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e function.\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":43686,"title":"Apply Function to Each Field of a Structure Array: Part 2","description":"The builtin \u003chttp://www.mathworks.com/help/matlab/ref/structfun.html?=structfun structfun\u003e applies a function handle to each field of a *scalar structure*, but it does not work for structure array. The purpose of this problem is to generalize structfun to enable support for structure array input.\r\n\r\nWrite your own function *structfun2* which accepts a structure array *s* and a function handle *f* as the inputs. The output *c = structfun2(f,s)* is a structure array *c* of the same size and same fields as *s*, where each field of the structure element of c stores the result of *f* applied to the same field of the relevant structure element in *s*. The structfun2 must preserve the behavior of structfun for scalar structure input. That is, when *s* is a scalar structure, your output reduces to c = structfun(f,s,'UniformOutput',false). No assumption on *f* is needed in this problem. \r\n \r\n\r\nExample: \r\n\r\n  s = struct('f1',{1, [3 4]; 1, [5 6]},'f2',{[1 2], 2; [3 4 5], [2 5]});\r\n  f = @numel;\r\n  c = struct('f1',{1, 2; 1, 2},'f2',{2, 1; 3, 2});\r\n\r\nRelated problems in this series:\r\n\r\n* \u003chttp://www.mathworks.com/matlabcentral/cody/problems/43685-apply-function-to-each-field-of-a-structure-array-part-1 Apply Function to Each Field of a Structure Array: Part 1\u003e\r\n* \u003chttp://www.mathworks.com/matlabcentral/cody/problems/43686-apply-function-to-each-field-of-a-structure-array-part-2 Apply Function to Each Field of a Structure Array: Part 2\u003e\r\n","description_html":"\u003cp\u003eThe builtin \u003ca href = \"http://www.mathworks.com/help/matlab/ref/structfun.html?=structfun\"\u003estructfun\u003c/a\u003e applies a function handle to each field of a \u003cb\u003escalar structure\u003c/b\u003e, but it does not work for structure array. The purpose of this problem is to generalize structfun to enable support for structure array input.\u003c/p\u003e\u003cp\u003eWrite your own function \u003cb\u003estructfun2\u003c/b\u003e which accepts a structure array \u003cb\u003es\u003c/b\u003e and a function handle \u003cb\u003ef\u003c/b\u003e as the inputs. The output \u003cb\u003ec = structfun2(f,s)\u003c/b\u003e is a structure array \u003cb\u003ec\u003c/b\u003e of the same size and same fields as \u003cb\u003es\u003c/b\u003e, where each field of the structure element of c stores the result of \u003cb\u003ef\u003c/b\u003e applied to the same field of the relevant structure element in \u003cb\u003es\u003c/b\u003e. The structfun2 must preserve the behavior of structfun for scalar structure input. That is, when \u003cb\u003es\u003c/b\u003e is a scalar structure, your output reduces to c = structfun(f,s,'UniformOutput',false). No assumption on \u003cb\u003ef\u003c/b\u003e is needed in this problem.\u003c/p\u003e\u003cp\u003eExample:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003es = struct('f1',{1, [3 4]; 1, [5 6]},'f2',{[1 2], 2; [3 4 5], [2 5]});\r\nf = @numel;\r\nc = struct('f1',{1, 2; 1, 2},'f2',{2, 1; 3, 2});\r\n\u003c/pre\u003e\u003cp\u003eRelated problems in this series:\u003c/p\u003e\u003cul\u003e\u003cli\u003e\u003ca href = \"http://www.mathworks.com/matlabcentral/cody/problems/43685-apply-function-to-each-field-of-a-structure-array-part-1\"\u003eApply Function to Each Field of a Structure Array: Part 1\u003c/a\u003e\u003c/li\u003e\u003cli\u003e\u003ca href = \"http://www.mathworks.com/matlabcentral/cody/problems/43686-apply-function-to-each-field-of-a-structure-array-part-2\"\u003eApply Function to Each Field of a Structure Array: Part 2\u003c/a\u003e\u003c/li\u003e\u003c/ul\u003e","function_template":"function y = structfun2(f,s)\r\n  y = s;\r\nend","test_suite":"%%\r\ns.f1 = 'Sunday';\r\ns.f2 = 'Monday';\r\ns.f3 = 'Tuesday'; \r\ns.f4 = 'Wednesday';\r\ns.f5 = 'Thursday';\r\ns.f6 = 'Friday';\r\ns.f7 = 'Saturday';\r\nf = @(x)x(1:3);\r\nc = structfun(f,s,'UniformOutput',false);\r\nassert(isequal(structfun2(f,s),c))\r\n\r\n%%\r\ns = struct('f1',{1, [3 4;5 6]; 1, [5 6;7 8]},'f2',{[1 2], 2; [3 4 5;6 7 8], [2 6;4 5]});\r\nf = @min;\r\nc = struct('f1',{1, [3 4]; 1, [5 6]},'f2',{1, 2; [3 4 5], [2 5]});\r\nassert(isequal(structfun2(f,s),c))\r\n\r\n%%\r\ns = struct('f1',{1, [3 4;5 6]; 1, [5 6;7 8]},'f2',{[1 2], 2; [3 4 5;6 7 8], [2 6;4 5]});\r\nf = @sum;\r\nc = struct('f1',{1, [8 10]; 1, [12 14]},'f2',{3, 2; [9 11 13], [6 11]});\r\nassert(isequal(structfun2(f,s),c))\r\n\r\n%%\r\ns = struct('f1',{1, [3 4;5 6]; 1, [5 6;7 8]},'f2',{[1 2], 2; [3 4 5;6 7 8], [2 6;4 5]});\r\nf = @pow2;\r\nc = struct('f1',{2, [8 16;32 64]; 2, [32 64;128 256]},'f2',{[2 4], 4; [8 16 32;64 128 256], [4 64;16 32]});\r\nassert(isequal(structfun2(f,s),c))\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":1,"created_by":12569,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":24,"test_suite_updated_at":"2016-12-01T22:27:07.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2016-11-27T02:12:26.000Z","updated_at":"2025-12-15T21:01:07.000Z","published_at":"2016-11-27T02:12: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\u003eThe builtin\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/help/matlab/ref/structfun.html?=structfun\\\"\u003e\u003cw:r\u003e\u003cw:t\u003estructfun\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e applies a function handle to each field of a\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003escalar structure\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, but it does not work for structure array. The purpose of this problem is to generalize structfun to enable support for structure array input.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eWrite your own function\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\u003estructfun2\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e which accepts a structure 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:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003es\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e and a function handle\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\u003ef\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e as the inputs. The output\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\u003ec = structfun2(f,s)\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e is a structure 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:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ec\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e of the same size and same fields as\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\u003es\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, where each field of the structure element of c stores the result of\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ef\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e applied to the same field of the relevant structure element 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:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003es\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e. The structfun2 must preserve the behavior of structfun for scalar structure input. That is, when\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\u003es\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e is a scalar structure, your output reduces to c = structfun(f,s,'UniformOutput',false). No assumption on\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\u003ef\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e is needed in this problem.\u003c/w:t\u003e\u003c/w:r\u003e\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[s = struct('f1',{1, [3 4]; 1, [5 6]},'f2',{[1 2], 2; [3 4 5], [2 5]});\\nf = @numel;\\nc = struct('f1',{1, 2; 1, 2},'f2',{2, 1; 3, 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\u003eRelated problems in this series:\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:hyperlink w:docLocation=\\\"http://www.mathworks.com/matlabcentral/cody/problems/43685-apply-function-to-each-field-of-a-structure-array-part-1\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eApply Function to Each Field of a Structure Array: Part 1\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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:hyperlink w:docLocation=\\\"http://www.mathworks.com/matlabcentral/cody/problems/43686-apply-function-to-each-field-of-a-structure-array-part-2\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eApply Function to Each Field of a Structure Array: Part 2\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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":1198,"title":"Handle to an array of functions","description":"Given a cell array of functions that operate on scalars, it is required to return a function handle to process a vector of values applying the functions successively (the first function operates on x, the second function operates on the resulting output, etc.).\r\n\r\nExample:\r\n\r\n \u003e\u003e f{1}=@(x) x^2;\r\n\r\n \u003e\u003e f{2}=@(x) x+3;\r\n\r\n \u003e\u003e f{3}=@(x) x/2;\r\n\r\n \u003e\u003e g=cf(f);\r\n\r\n \u003e\u003e x=[1 2 3];\r\n\r\n \u003e\u003e g(x)\r\n\r\n ans =\r\n\r\n     2.0000    3.5000    6.0000\r\n ","description_html":"\u003cp\u003eGiven a cell array of functions that operate on scalars, it is required to return a function handle to process a vector of values applying the functions successively (the first function operates on x, the second function operates on the resulting output, etc.).\u003c/p\u003e\u003cp\u003eExample:\u003c/p\u003e\u003cpre\u003e \u003e\u003e f{1}=@(x) x^2;\u003c/pre\u003e\u003cpre\u003e \u003e\u003e f{2}=@(x) x+3;\u003c/pre\u003e\u003cpre\u003e \u003e\u003e f{3}=@(x) x/2;\u003c/pre\u003e\u003cpre\u003e \u003e\u003e g=cf(f);\u003c/pre\u003e\u003cpre\u003e \u003e\u003e x=[1 2 3];\u003c/pre\u003e\u003cpre\u003e \u003e\u003e g(x)\u003c/pre\u003e\u003cpre\u003e ans =\u003c/pre\u003e\u003cpre\u003e     2.0000    3.5000    6.0000\u003c/pre\u003e","function_template":"function y = cf(x)\r\n  y = x;\r\nend","test_suite":"%%\r\nf{1}=@(x) x^2;\r\nf{2}=@(x) x+3;\r\nf{3}=@(x) x/2;\r\ng=cf(f);\r\nx=[1 2 3];\r\ny_correct = [2 3.5 6];\r\nassert(isequal(g(x),y_correct))\r\n%%\r\nf{1}=@(x) x^0.5;\r\nf{2}=@(x) x-1;\r\nf{3}=@(x) x^2;\r\nf{4}=@(x) x/3;\r\ng=cf(f);\r\nx=[ 16 49 100];\r\ny_correct = [3 12 27];\r\nassert(isequal(g(x),y_correct))\r\n","published":true,"deleted":false,"likes_count":6,"comments_count":1,"created_by":3399,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":61,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2013-01-11T18:06:01.000Z","updated_at":"2025-12-10T11:02:26.000Z","published_at":"2013-01-11T18:06:25.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eGiven a cell array of functions that operate on scalars, it is required to return a function handle to process a vector of values applying the functions successively (the first function operates on x, the second function operates on the resulting output, etc.).\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExample:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ \u003e\u003e f{1}=@(x) x^2;\\n\\n \u003e\u003e f{2}=@(x) x+3;\\n\\n \u003e\u003e f{3}=@(x) x/2;\\n\\n \u003e\u003e g=cf(f);\\n\\n \u003e\u003e x=[1 2 3];\\n\\n \u003e\u003e g(x)\\n\\n ans =\\n\\n     2.0000    3.5000    6.0000]]\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":44808,"title":"Make an anonymous function that has variable output","description":"Make a anonymous function that has variable output.\r\n\r\n\r\nf = @(x)...\r\n\r\nthe following equation→equation(s) as followed has(ve)\r\n\r\na = f(x) ==\u003e a = x{1}\r\n\r\n[a,b] = f(x) ==\u003e a = x{1}, b = x{2}\r\n\r\n...\r\n\r\nYou can see the test suite for details.\r\n","description_html":"\u003cp\u003eMake a anonymous function that has variable output.\u003c/p\u003e\u003cp\u003ef = @(x)...\u003c/p\u003e\u003cp\u003ethe following equation→equation(s) as followed has(ve)\u003c/p\u003e\u003cp\u003ea = f(x) ==\u0026gt; a = x{1}\u003c/p\u003e\u003cp\u003e[a,b] = f(x) ==\u0026gt; a = x{1}, b = x{2}\u003c/p\u003e\u003cp\u003e...\u003c/p\u003e\u003cp\u003eYou can see the test suite for details.\u003c/p\u003e","function_template":"function y = anonymous(x)\r\n  y = x;\r\nend","test_suite":"%% 1\r\nfid = fopen('anonymous.m');\r\nst = regexprep(char(fread(fid)'), 'function', 'error(''No fancy functions!''); %','ignorecase',2);\r\nfclose(fid);\r\nfid = fopen('anonymous.m' , 'w');\r\nfwrite(fid,st);\r\nfclose(fid);\r\n%% 2\r\nf = anonymous;\r\na = f(1);\r\nassert(a==1);\r\n%% 3\r\nf = anonymous;\r\na = f(1,2);\r\nassert(a==1);\r\n[a,b] = f(3,5);\r\nassert(a==3\u0026b==5);\r\n%% 4\r\nf = anonymous;\r\na = f(1,2);\r\nassert(a==1);\r\n[a,b] = f(3,'56');\r\nassert(a==3\u0026isequal(b,'56'));\r\n\r\n\r\n","published":true,"deleted":false,"likes_count":2,"comments_count":1,"created_by":3668,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":31,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2018-12-02T13:36:44.000Z","updated_at":"2025-12-14T20:24:59.000Z","published_at":"2018-12-02T13:36:49.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\u003eMake a anonymous function that has variable output.\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\u003ef = @(x)...\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\u003ethe following equation→equation(s) as followed has(ve)\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\u003ea = f(x) ==\u0026gt; a = x{1}\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e[a,b] = f(x) ==\u0026gt; a = x{1}, b = x{2}\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\u003e...\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYou can see the test suite for details.\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":583,"title":"Implement a counter","description":"Write a function that returns a function that counts the number of times it is invoked. Example:\r\n\r\n  \u003e\u003e h = counter;\r\n  \u003e\u003e h()\r\n  ans =\r\n       1\r\n  \u003e\u003e h()\r\n  ans =\r\n       2\r\n  \u003e\u003e h()\r\n  ans =\r\n       3\r\n\r\nNote: your solution *cannot* use persistent variables.","description_html":"\u003cp\u003eWrite a function that returns a function that counts the number of times it is invoked. Example:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e\u003e\u003e h = counter;\r\n\u003e\u003e h()\r\nans =\r\n     1\r\n\u003e\u003e h()\r\nans =\r\n     2\r\n\u003e\u003e h()\r\nans =\r\n     3\r\n\u003c/pre\u003e\u003cp\u003eNote: your solution \u003cb\u003ecannot\u003c/b\u003e use persistent variables.\u003c/p\u003e","function_template":"function h = counter;\r\nend","test_suite":"%%\r\nh = counter;\r\nassert(isequal(h(), 1))\r\nassert(isequal(h(), 2))\r\nassert(isequal(h(), 3))\r\nassert(isequal(h(), 4))\r\nassert(isequal(h(), 5))\r\n\r\n%%\r\ncode = fileread('counter.m');\r\nassert(isempty(strfind(code, 'persistent')));","published":true,"deleted":false,"likes_count":6,"comments_count":0,"created_by":19,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":102,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2012-04-13T18:59:04.000Z","updated_at":"2025-12-11T12:06:28.000Z","published_at":"2012-04-13T19:27:45.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\u003eWrite a function that returns a function that counts the number of times it is invoked. 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[\u003e\u003e h = counter;\\n\u003e\u003e h()\\nans =\\n     1\\n\u003e\u003e h()\\nans =\\n     2\\n\u003e\u003e h()\\nans =\\n     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\u003eNote: your solution\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\u003ecannot\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e use persistent variables.\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":1224,"title":"Flexible Anonymous Function","description":"Given a function handle, return a handle to a function that would accept an arbitrary number of inputs, applies the function to each input, and returns an arbitrary number of (respective) outputs (\u003c= the number of inputs, of course).\r\n\r\nExample:\r\n\r\n myf=@(x) det(x);\r\n\r\n yourf = flexf(myf);\r\n\r\n [a,~,b,c] = yourf([1 2;3 4],7,[1 2 3; 4 5 6; 2 3 1],3,[2 -1 ; 1 -1])\r\n\r\n a = -2\r\n b = 9\r\n c = 3 \r\n\r\n","description_html":"\u003cp\u003eGiven a function handle, return a handle to a function that would accept an arbitrary number of inputs, applies the function to each input, and returns an arbitrary number of (respective) outputs (\u0026lt;= the number of inputs, of course).\u003c/p\u003e\u003cp\u003eExample:\u003c/p\u003e\u003cpre\u003e myf=@(x) det(x);\u003c/pre\u003e\u003cpre\u003e yourf = flexf(myf);\u003c/pre\u003e\u003cpre\u003e [a,~,b,c] = yourf([1 2;3 4],7,[1 2 3; 4 5 6; 2 3 1],3,[2 -1 ; 1 -1])\u003c/pre\u003e\u003cpre\u003e a = -2\r\n b = 9\r\n c = 3 \u003c/pre\u003e","function_template":"function y = flexf(f)\r\n  y = f;\r\nend","test_suite":"%%\r\nmyf=@(x) det(x);\r\nyourf = flexf(myf);\r\nx={[1 2;3 4],7,[1 2 3; 4 5 6; 2 3 1],3,[2 -1 ; 1 -1]};\r\n[a,~,b,c]=yourf(x{:});\r\ny_correct = {-2 9 3};\r\nassert(isequal({a b c},y_correct));\r\n%%\r\nmyf=@(x) x.^2;\r\nyourf = flexf(myf);\r\nx={[1 2;3 4],7,[1 2 3; 4 5 6; 2 3 1],3,[2 -1 ; 1 -1]};\r\n[a,~,b,c]=yourf(x{:});\r\ny_correct = cellfun(myf,x,'uni',0);\r\nassert(isequal({a b c},y_correct([1 3 4])));\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":0,"created_by":3399,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":35,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2013-01-23T21:46:41.000Z","updated_at":"2025-12-15T22:03:18.000Z","published_at":"2013-01-23T22:04:52.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 function handle, return a handle to a function that would accept an arbitrary number of inputs, applies the function to each input, and returns an arbitrary number of (respective) outputs (\u0026lt;= the number of inputs, of course).\u003c/w:t\u003e\u003c/w:r\u003e\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[ myf=@(x) det(x);\\n\\n yourf = flexf(myf);\\n\\n [a,~,b,c] = yourf([1 2;3 4],7,[1 2 3; 4 5 6; 2 3 1],3,[2 -1 ; 1 -1])\\n\\n a = -2\\n b = 9\\n c = 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":24,"title":"Function Iterator","description":"Given a handle fh to a function which takes a scalar input and returns a scalar output and an integer n \u003e= 1, return a  handle fh2 to a function which applies the given function n times.\n\nExamples:\n\n \u003e\u003e addOne = @(x)x+1;\n \u003e\u003e addTen = iterate_fcn(addOne, 10);\n \u003e\u003e addTen(3)\n ans =\n     13\n\n \u003e\u003e squarer = @(a) a^2;\n \u003e\u003e fh2 = iterate_fcn(squarer, 3);\n \u003e\u003e fh2(3)\n ans =\n         6561\n\n % Golden Ratio\n \u003e\u003e fh = @(y)sqrt(y+1);\n \u003e\u003e fh2 = iterate_fcn(fh,30);\n \u003e\u003e fh2(1)\n ans =\n     1.6180\n","description_html":"\u003cp\u003eGiven a handle fh to a function which takes a scalar input and returns a scalar output and an integer n \u003e= 1, return a  handle fh2 to a function which applies the given function n times.\u003c/p\u003e\u003cp\u003eExamples:\u003c/p\u003e\u003cpre\u003e \u003e\u003e addOne = @(x)x+1;\n \u003e\u003e addTen = iterate_fcn(addOne, 10);\n \u003e\u003e addTen(3)\n ans =\n     13\u003c/pre\u003e\u003cpre\u003e \u003e\u003e squarer = @(a) a^2;\n \u003e\u003e fh2 = iterate_fcn(squarer, 3);\n \u003e\u003e fh2(3)\n ans =\n         6561\u003c/pre\u003e\u003cpre\u003e % Golden Ratio\n \u003e\u003e fh = @(y)sqrt(y+1);\n \u003e\u003e fh2 = iterate_fcn(fh,30);\n \u003e\u003e fh2(1)\n ans =\n     1.6180\u003c/pre\u003e","function_template":"function fh2 = iterate_fcn(fh, n)\nfh2 = fh;\nend","test_suite":"%%\nnoOp = @(x)x;\nfh2 = iterate_fcn(noOp, 50);\nassert(isequal(fh2(pi),pi));\n\n\n%%\naddOne = @(x)x+1;\naddTen = iterate_fcn(addOne, 10);\nassert(isequal(addTen(3),13));\n\n%%\naddOne = @(x)x+1;\naddOne2 = iterate_fcn(addOne, 1);\nassert(isequal(addOne2(3),4));\n\n%%\nsquarer = @(a) a^2;\nfh2 = iterate_fcn(squarer, 3);\nassert(isequal(fh2(3),6561));\n\n%%\nfh = @(y)sqrt(y+1);\nfh2 = iterate_fcn(fh,30);\nassert(abs(fh2(1) - (1+sqrt(5))/2) \u003c 100*eps);","published":true,"deleted":false,"likes_count":61,"comments_count":27,"created_by":1,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":2244,"test_suite_updated_at":"2012-01-18T01:00:20.000Z","rescore_all_solutions":false,"group_id":2,"created_at":"2012-01-18T01:00:20.000Z","updated_at":"2026-03-15T20:56:03.000Z","published_at":"2012-01-18T01:00:20.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eGiven a handle fh to a function which takes a scalar input and returns a scalar output and an integer n \u0026gt;= 1, return a handle fh2 to a function which applies the given function n times.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExamples:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ \u003e\u003e addOne = @(x)x+1;\\n \u003e\u003e addTen = iterate_fcn(addOne, 10);\\n \u003e\u003e addTen(3)\\n ans =\\n     13\\n\\n \u003e\u003e squarer = @(a) a^2;\\n \u003e\u003e fh2 = iterate_fcn(squarer, 3);\\n \u003e\u003e fh2(3)\\n ans =\\n         6561\\n\\n % Golden Ratio\\n \u003e\u003e fh = @(y)sqrt(y+1);\\n \u003e\u003e fh2 = iterate_fcn(fh,30);\\n \u003e\u003e fh2(1)\\n ans =\\n     1.6180]]\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":733,"title":"Extract Built In Functions and Toolbox Functions from String or Function Handle","description":"Find the Built-In functions and Toolbox functions in either a string or a function handle.\r\n\r\nGenerate a string of alphabetized Built-In functions followed by alphabetized Functions.\r\n\r\n*Inputs:*\r\n\r\nfh=@(x)log10(x)+log2(x)+abs(x)\r\n\r\nstr='smooth3(x,y)-filter(x)+abs(n)+filter2(u)+sin(x)+numel(z)'\r\n\r\n*Outputs:*\r\n\r\n'abs log2 log10'\r\n\r\n'abs filter numel sin filter2 smooth3'\r\n\r\nRelated to \r\n\u003chttp://www.mathworks.com/matlabcentral/cody/problems/464-function-sniffer Cody_464\u003e","description_html":"\u003cp\u003eFind the Built-In functions and Toolbox functions in either a string or a function handle.\u003c/p\u003e\u003cp\u003eGenerate a string of alphabetized Built-In functions followed by alphabetized Functions.\u003c/p\u003e\u003cp\u003e\u003cb\u003eInputs:\u003c/b\u003e\u003c/p\u003e\u003cp\u003efh=@(x)log10(x)+log2(x)+abs(x)\u003c/p\u003e\u003cp\u003estr='smooth3(x,y)-filter(x)+abs(n)+filter2(u)+sin(x)+numel(z)'\u003c/p\u003e\u003cp\u003e\u003cb\u003eOutputs:\u003c/b\u003e\u003c/p\u003e\u003cp\u003e'abs log2 log10'\u003c/p\u003e\u003cp\u003e'abs filter numel sin filter2 smooth3'\u003c/p\u003e\u003cp\u003eRelated to  \u003ca href=\"http://www.mathworks.com/matlabcentral/cody/problems/464-function-sniffer\"\u003eCody_464\u003c/a\u003e\u003c/p\u003e","function_template":"function functions = find_functions(fh_str)\r\n  functions = '';\r\nend","test_suite":"%%\r\nfh_str='log2(x)+smooth3(x,y)+abs(2)+log10(5)';\r\nexp_str='abs log10 log2 smooth3';\r\nassert(isequal(find_functions(fh_str),exp_str))\r\n%%\r\nfh_str='for k=log10(x):log2(x)+abs(x)';\r\nexp_str='abs for log10 log2';\r\nassert(isequal(find_functions(fh_str),exp_str))\r\n%%\r\nfh_str=@(x)x^2+sin(x)-cos(x);\r\nexp_str='cos sin';\r\nassert(isequal(find_functions(fh_str),exp_str))\r\n%%\r\nfh_str='@(x)x^2+sin(x)-cos(x)';\r\nexp_str='cos sin';\r\nassert(isequal(find_functions(fh_str),exp_str))\r\n%%\r\nfh_str='filter2(x,A)+filter(x)-cos(x) expm(z)';\r\nexp_str='cos filter expm filter2';\r\nassert(isequal(find_functions(fh_str),exp_str))\r\n%%\r\nfh_str='smooth3(x,y)-filter(x)+abs(n)+filter2(u)+sin(x)+numel(z)';\r\nexp_str='abs filter numel sin filter2 smooth3';\r\nassert(isequal(find_functions(fh_str),exp_str))\r\n","published":true,"deleted":false,"likes_count":5,"comments_count":6,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":83,"test_suite_updated_at":"2012-07-18T13:18:16.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2012-06-01T23:09:01.000Z","updated_at":"2026-03-31T20:12:36.000Z","published_at":"2012-06-02T00:17:41.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eFind the Built-In functions and Toolbox functions in either a string or a function handle.\u003c/w:t\u003e\u003c/w:r\u003e\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\u003eGenerate a string of alphabetized Built-In functions followed by alphabetized Functions.\u003c/w:t\u003e\u003c/w:r\u003e\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\u003eInputs:\u003c/w:t\u003e\u003c/w:r\u003e\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\u003efh=@(x)log10(x)+log2(x)+abs(x)\u003c/w:t\u003e\u003c/w:r\u003e\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\u003estr='smooth3(x,y)-filter(x)+abs(n)+filter2(u)+sin(x)+numel(z)'\u003c/w:t\u003e\u003c/w:r\u003e\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\u003eOutputs:\u003c/w:t\u003e\u003c/w:r\u003e\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'abs log2 log10'\u003c/w:t\u003e\u003c/w:r\u003e\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'abs filter numel sin filter2 smooth3'\u003c/w:t\u003e\u003c/w:r\u003e\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\u003eRelated to \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/464-function-sniffer\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eCody_464\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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":42447,"title":"Define the operators of  function_handles","description":"Suppose f and g are function_handles, try to define f+g,f-g,f*g and f/g.\r\n     \r\n  e.g. \r\n     if   f = @(x)x and g = @(x)x+1\r\n     then\r\n          f+g = @(x)2*x+1\r\n          f-g = @(x)-1\r\n          f*g = @(x)x*(x+1)\r\n          f/g = @(x)x/(x+1)","description_html":"\u003cp\u003eSuppose f and g are function_handles, try to define f+g,f-g,f*g and f/g.\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003ee.g. \r\n   if   f = @(x)x and g = @(x)x+1\r\n   then\r\n        f+g = @(x)2*x+1\r\n        f-g = @(x)-1\r\n        f*g = @(x)x*(x+1)\r\n        f/g = @(x)x/(x+1)\r\n\u003c/pre\u003e","function_template":"function y = fho(f,g,type)\r\n  % 'type' define which operation to be performed\r\n  % for simplicity, 1 represents '+', 2 represents '-', 3 represents '*', 4 represents '/'.\r\n  y = x;\r\nend","test_suite":"%%\r\nf = @(x)x;\r\ng = @(x)x+1;\r\ny = fho(f,g,1)\r\ny_correct = 3;\r\nassert(isequal(y(1),y_correct))\r\n\r\n%%\r\nf = @sqrt;\r\ng = @ceil;\r\ny = fho(f,g,2)\r\ny_correct = -2;\r\nassert(isequal(y(4),y_correct))\r\n\r\n%%\r\nf = @nextpow2;\r\ng = @log10;\r\ny = fho(f,g,3)\r\ny_correct = 4;\r\nassert(isequal(y(10),y_correct))\r\n\r\n%%\r\nf = @floor;\r\ng = @ceil;\r\ny = fho(f,g,4)\r\ny_correct = 0.5;\r\nassert(isequal(y(1.25),y_correct))","published":true,"deleted":false,"likes_count":0,"comments_count":0,"created_by":40597,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":58,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2015-07-05T14:27:43.000Z","updated_at":"2026-03-31T11:28:04.000Z","published_at":"2015-07-05T15:08:46.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\u003eSuppose f and g are function_handles, try to define f+g,f-g,f*g and f/g.\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[e.g. \\n   if   f = @(x)x and g = @(x)x+1\\n   then\\n        f+g = @(x)2*x+1\\n        f-g = @(x)-1\\n        f*g = @(x)x*(x+1)\\n        f/g = @(x)x/(x+1)]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":581,"title":"Function composition","description":"Write a function that accepts two function handles f and g and returns the composition h. That is,\r\n\r\nh = (f o g)(x) = f(g(x))\r\n\r\nExample:\r\n\r\n  \u003e\u003e f = @(x)x^2;\r\n  \u003e\u003e g = @(x)x+1;\r\n  \u003e\u003e h = composeFcn(f,g);\r\n  \u003e\u003e h(3)\r\n  ans =\r\n    16\r\n    \r\nbecause (3+1)^2 = 16.","description_html":"\u003cp\u003eWrite a function that accepts two function handles f and g and returns the composition h. That is,\u003c/p\u003e\u003cp\u003eh = (f o g)(x) = f(g(x))\u003c/p\u003e\u003cp\u003eExample:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e\u0026gt;\u0026gt; f = @(x)x^2;\r\n\u0026gt;\u0026gt; g = @(x)x+1;\r\n\u0026gt;\u0026gt; h = composeFcn(f,g);\r\n\u0026gt;\u0026gt; h(3)\r\nans =\r\n  16\r\n\u003c/pre\u003e\u003cp\u003ebecause (3+1)^2 = 16.\u003c/p\u003e","function_template":"function h = composeFcn(f,g)\r\nend","test_suite":"%%\r\nf = @(x)x^2;\r\ng = @(x)x+1;\r\nh = composeFcn(f,g);\r\nassert(isequal(h(3),16));\r\n\r\n%%\r\nf = @round;\r\ng = @sqrt;\r\nh = composeFcn(f,g);\r\nassert(isequal(h(8),3));","published":true,"deleted":false,"likes_count":1,"comments_count":8,"created_by":19,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":261,"test_suite_updated_at":"2017-04-26T13:06:48.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2012-04-13T18:57:19.000Z","updated_at":"2026-04-02T10:42:01.000Z","published_at":"2012-04-13T19:27:06.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eWrite a function that accepts two function handles f and g and returns the composition h. That is,\u003c/w:t\u003e\u003c/w:r\u003e\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\u003eh = (f o g)(x) = f(g(x))\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExample:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[\u003e\u003e f = @(x)x^2;\\n\u003e\u003e g = @(x)x+1;\\n\u003e\u003e h = composeFcn(f,g);\\n\u003e\u003e h(3)\\nans =\\n  16]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ebecause (3+1)^2 = 16.\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":44083,"title":"First use of arrayfun() and anonymous function @(x)","description":"Create an anonymous function using @(x) for a parabola equation for the given coefficients stored in s with\r\ns(1)x2 + s(2)x + s(3).\r\nUse arrayfun() to apply the parabola equation to each element in the array A.\r\nNote: for , while , and eval are forbidden.\r\nHave fun :o)","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: 141px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 407px 70.5px; transform-origin: 407px 70.5px; vertical-align: baseline; \"\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 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: 337px 8px; transform-origin: 337px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eCreate an anonymous function using @(x) for a parabola equation for the given coefficients stored in s with\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: 65.5px 8px; transform-origin: 65.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003es(1)x2 + s(2)x + s(3).\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: 243px 8px; transform-origin: 243px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eUse arrayfun() to apply the parabola equation to each element in the array A.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 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: 16.5px 8px; transform-origin: 16.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eNote:\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 2px 8px; transform-origin: 2px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 8.5px 8px; transform-origin: 8.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"font-style: italic; \"\u003efor\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 4px 8px; transform-origin: 4px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e ,\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 2px 8px; transform-origin: 2px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 15.5px 8px; transform-origin: 15.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"font-style: italic; \"\u003ewhile\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 18px 8px; transform-origin: 18px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e , and\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 2px 8px; transform-origin: 2px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 12.5px 8px; transform-origin: 12.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"font-style: italic; \"\u003eeval\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 46.5px 8px; transform-origin: 46.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e are forbidden.\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: 38.5px 8px; transform-origin: 38.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eHave fun :o)\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function B = parabola_equation(A,s)\r\n    \r\n  B = A;\r\n  \r\nend","test_suite":"%%\r\nA = [0 1 2];\r\ns = [1 2 3];\r\ny_correct = [3 6 11];\r\nassert(isequal(parabola_equation(A,s),y_correct))\r\n%%\r\nA = -12:4:11;\r\ns = [-5 -8 0];\r\ny_correct = [-624  -256   -48     0  -112  -384];\r\nassert(isequal(parabola_equation(A,s),y_correct))\r\n%%\r\nA = -2:2;\r\ns = [0 pi 0];\r\ny_correct = [-2*pi -pi 0  pi 2*pi];\r\nassert(isequal(parabola_equation(A,s),y_correct))\r\n%%\r\nassert(isempty(regexp(evalc('type parabola_equation'),'(eval|for|while|polyval|)')))\r\nassert(not(isempty(regexp(evalc('type parabola_equation'),'(@)'))))\r\nassert(not(isempty(regexp(evalc('type parabola_equation'),'arrayfun'))))\r\n","published":true,"deleted":false,"likes_count":0,"comments_count":1,"created_by":100084,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":72,"test_suite_updated_at":"2022-03-01T19:46:58.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2017-03-10T22:47:36.000Z","updated_at":"2026-03-16T13:46:36.000Z","published_at":"2017-03-10T22:48:28.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eCreate an anonymous function using @(x) for a parabola equation for the given coefficients stored in s with\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\u003es(1)x2 + s(2)x + s(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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eUse arrayfun() to apply the parabola equation to each element in the array A.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eNote:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003efor\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: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\u003ewhile\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:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eeval\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e are forbidden.\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\u003eHave fun :o)\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":260,"title":"Create a function handle that reverses the input arguments of another function handle","description":"Given a function that takes two input arguments and returns one output, create another function handle that performs the same operation but with the input arguments in reverse order.\r\n\r\nFor example:\r\n\r\n   f = @(x,y) 2*x+y;\r\n   f(5,6)\r\n\r\nreturns the answer 16. Your function should produce another function handle that performs the same operation except that y is the first input argument and x is the second.\r\n\r\n   g = reverseArguments(f);\r\n   g(6,5)\r\n\r\nreturns 16, and:\r\n\r\n   g(5,6)\r\n\r\nreturns 17.\r\n","description_html":"\u003cp\u003eGiven a function that takes two input arguments and returns one output, create another function handle that performs the same operation but with the input arguments in reverse order.\u003c/p\u003e\u003cp\u003eFor example:\u003c/p\u003e\u003cpre\u003e   f = @(x,y) 2*x+y;\r\n   f(5,6)\u003c/pre\u003e\u003cp\u003ereturns the answer 16. Your function should produce another function handle that performs the same operation except that y is the first input argument and x is the second.\u003c/p\u003e\u003cpre\u003e   g = reverseArguments(f);\r\n   g(6,5)\u003c/pre\u003e\u003cp\u003ereturns 16, and:\u003c/p\u003e\u003cpre\u003e   g(5,6)\u003c/pre\u003e\u003cp\u003ereturns 17.\u003c/p\u003e","function_template":"function g = reverseArguments(f)\r\n  g = @sin;\r\nend","test_suite":"%%\r\nf = @(x,y) 2*x+y;\r\ng = reverseArguments(f);\r\nassert(isequal(g(5,6), 17));\r\nassert(isequal(g(6,5), 16));\r\n\r\n%%\r\nf = @(x,y) x.^y;\r\ng = reverseArguments(f);\r\nassert(isequal(g(2,3), 9));\r\n\r\n%%\r\nf = @(A,theta) A*sin(theta);\r\ng = reverseArguments(f);\r\nassert(isequal(g(4,10), 10*sin(4)));\r\n","published":true,"deleted":false,"likes_count":3,"comments_count":0,"created_by":4303371,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":157,"test_suite_updated_at":"2012-02-05T02:33:43.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2012-02-05T02:33:43.000Z","updated_at":"2025-12-15T21:13:28.000Z","published_at":"2012-02-05T02:34:46.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 function that takes two input arguments and returns one output, create another function handle that performs the same operation but with the input arguments in reverse order.\u003c/w:t\u003e\u003c/w:r\u003e\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[   f = @(x,y) 2*x+y;\\n   f(5,6)]]\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 answer 16. Your function should produce another function handle that performs the same operation except that y is the first input argument and x is the second.\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[   g = reverseArguments(f);\\n   g(6,5)]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ereturns 16, and:\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[   g(5,6)]]\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 17.\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":1196,"title":"Apply a function array to an array of numbers ","description":"It is required to apply a cell array of functions to a numerical array, where the functions accept only scalar inputs.\r\n\r\nExample:\r\n\r\n\u003e\u003e f{1} = @(x) x;\r\n\r\n\u003e\u003e f{2} = @(x) x^2;\r\n\r\n\u003e\u003e f{3} = @(x) x^3;\r\n\r\n\u003e\u003e x = 1:5;\r\n\r\n\u003e\u003e arfn(f,x)\r\n\r\nans =\r\n\r\n     1     2     3     4     5\r\n     1     4     9    16    25\r\n     1     8    27    64   125","description_html":"\u003cp\u003eIt is required to apply a cell array of functions to a numerical array, where the functions accept only scalar inputs.\u003c/p\u003e\u003cp\u003eExample:\u003c/p\u003e\u003cp\u003e\u003e\u003e f{1} = @(x) x;\u003c/p\u003e\u003cp\u003e\u003e\u003e f{2} = @(x) x^2;\u003c/p\u003e\u003cp\u003e\u003e\u003e f{3} = @(x) x^3;\u003c/p\u003e\u003cp\u003e\u003e\u003e x = 1:5;\u003c/p\u003e\u003cp\u003e\u003e\u003e arfn(f,x)\u003c/p\u003e\u003cp\u003eans =\u003c/p\u003e\u003cpre\u003e     1     2     3     4     5\r\n     1     4     9    16    25\r\n     1     8    27    64   125\u003c/pre\u003e","function_template":"function y = arfn(f,x)\r\n  y = x;\r\nend","test_suite":"%%\r\nf{1} = @(x) x;\r\nf{2} = @(x) x^2;\r\nf{3} = @(x) x^3;\r\nx = 1:5;\r\ny_correct = [     1     2     3     4     5\r\n                  1     4     9    16    25\r\n                  1     8    27    64   125];\r\nassert(isequal(arfn(f,x),y_correct))\r\n\r\n%%\r\nf{1} = @(x) 1/x;\r\nf{2} = @(x) x^0.5;\r\nf{3} = @(x) x-4;\r\nx = [4 16 25 100];\r\ny_correct = [0.25    0.0625    0.04    0.01\r\n             2       4         5       10\r\n             0       12        21      96];\r\nassert(isequal(arfn(f,x),y_correct))\r\n","published":true,"deleted":false,"likes_count":2,"comments_count":0,"created_by":3399,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":102,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2013-01-11T11:58:13.000Z","updated_at":"2025-12-19T04:32:02.000Z","published_at":"2013-01-11T12:08:22.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eIt is required to apply a cell array of functions to a numerical array, where the functions accept only scalar inputs.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExample:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u0026gt;\u0026gt; f{1} = @(x) x;\u003c/w:t\u003e\u003c/w:r\u003e\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\u0026gt;\u0026gt; f{2} = @(x) x^2;\u003c/w:t\u003e\u003c/w:r\u003e\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\u0026gt;\u0026gt; f{3} = @(x) x^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\u003e\u0026gt;\u0026gt; x = 1: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\u003e\u0026gt;\u0026gt; arfn(f,x)\u003c/w:t\u003e\u003c/w:r\u003e\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\u003eans =\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[     1     2     3     4     5\\n     1     4     9    16    25\\n     1     8    27    64   125]]\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":429,"title":"function on a moving window","description":"Create a function that applies an operation (such as @sum, @mean, @std, @norm etc) to a moving window of the data.\r\nFirst example:\r\n    filtered = mopt(@mean,1:6,1,2)\r\n\r\n    Then filtered = [NaN 2.5000 3.5000 4.5000 NaN NaN]\r\nThis is the moving average.\r\nSecond example:\r\n    filtered = mopt(@std,[0.2 0.8 0.7 1.1 1.1 1.0 0.2],2,0)\r\n\r\n    Then filtered = [NaN NaN 0.3215 0.2082 0.2309 0.0577 0.4933]\r\nThis is the 'moving standard error'.\r\nThe first arg of mopt is a function handle. It must be a function that takes a vector as input and produces a scalar as output.\r\nThe second arg is the vector of data.\r\nThe third and fourth args are the lags and leads that determin the size of the moving window.","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: 396.6px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 407px 198.3px; transform-origin: 407px 198.3px; 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: 377px 8px; transform-origin: 377px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eCreate a function that applies an operation (such as @sum, @mean, @std, @norm etc) to a moving window of the data.\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: 44px 8px; transform-origin: 44px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eFirst example:\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgb(247, 247, 247); block-size: 61.3px; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-end-end-radius: 4px; border-end-start-radius: 4px; border-start-end-radius: 4px; border-start-start-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; margin-block-end: 10px; margin-block-start: 10px; margin-bottom: 10px; margin-inline-end: 3px; margin-inline-start: 3px; margin-left: 3px; margin-right: 3px; margin-top: 10px; perspective-origin: 404px 30.65px; transform-origin: 404px 30.65px; margin-left: 3px; margin-top: 10px; margin-bottom: 10px; margin-right: 3px; \"\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 136px 8.5px; tab-size: 4; transform-origin: 136px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e    filtered = mopt(@mean,1:6,1,2)\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 0px 8.5px; tab-size: 4; transform-origin: 0px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 216px 8.5px; tab-size: 4; transform-origin: 216px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; perspective-origin: 36px 8.5px; transform-origin: 36px 8.5px; \"\u003e    Then \u003c/span\u003e\u003cspan style=\"border-block-end-color: rgb(170, 4, 249); border-block-start-color: rgb(170, 4, 249); border-bottom-color: rgb(170, 4, 249); border-inline-end-color: rgb(170, 4, 249); border-inline-start-color: rgb(170, 4, 249); border-left-color: rgb(170, 4, 249); border-right-color: rgb(170, 4, 249); border-top-color: rgb(170, 4, 249); caret-color: rgb(170, 4, 249); color: rgb(170, 4, 249); column-rule-color: rgb(170, 4, 249); margin-inline-end: 0px; margin-right: 0px; outline-color: rgb(170, 4, 249); perspective-origin: 180px 8.5px; text-decoration-color: rgb(170, 4, 249); text-emphasis-color: rgb(170, 4, 249); transform-origin: 180px 8.5px; \"\u003efiltered = [NaN 2.5000 3.5000 4.5000 NaN NaN]\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 10px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 10px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 10px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 87px 8px; transform-origin: 87px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eThis is the moving average.\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: 54px 8px; transform-origin: 54px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eSecond example:\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgb(247, 247, 247); block-size: 61.3px; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-end-end-radius: 4px; border-end-start-radius: 4px; border-start-end-radius: 4px; border-start-start-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; margin-block-end: 10px; margin-block-start: 10px; margin-bottom: 10px; margin-inline-end: 3px; margin-inline-start: 3px; margin-left: 3px; margin-right: 3px; margin-top: 10px; perspective-origin: 404px 30.65px; transform-origin: 404px 30.65px; margin-left: 3px; margin-top: 10px; margin-bottom: 10px; margin-right: 3px; \"\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 236px 8.5px; tab-size: 4; transform-origin: 236px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e    filtered = mopt(@std,[0.2 0.8 0.7 1.1 1.1 1.0 0.2],2,0)\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 0px 8.5px; tab-size: 4; transform-origin: 0px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 256px 8.5px; tab-size: 4; transform-origin: 256px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; perspective-origin: 36px 8.5px; transform-origin: 36px 8.5px; \"\u003e    Then \u003c/span\u003e\u003cspan style=\"border-block-end-color: rgb(170, 4, 249); border-block-start-color: rgb(170, 4, 249); border-bottom-color: rgb(170, 4, 249); border-inline-end-color: rgb(170, 4, 249); border-inline-start-color: rgb(170, 4, 249); border-left-color: rgb(170, 4, 249); border-right-color: rgb(170, 4, 249); border-top-color: rgb(170, 4, 249); caret-color: rgb(170, 4, 249); color: rgb(170, 4, 249); column-rule-color: rgb(170, 4, 249); margin-inline-end: 0px; margin-right: 0px; outline-color: rgb(170, 4, 249); perspective-origin: 220px 8.5px; text-decoration-color: rgb(170, 4, 249); text-emphasis-color: rgb(170, 4, 249); transform-origin: 220px 8.5px; \"\u003efiltered = [NaN NaN 0.3215 0.2082 0.2309 0.0577 0.4933]\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 10px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 10px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 10px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 109.5px 8px; transform-origin: 109.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eThis is the 'moving standard error'.\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: 366.5px 8px; transform-origin: 366.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eThe first arg of mopt is a function handle. It must be a function that takes a vector as input and produces a scalar as output.\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: 116.5px 8px; transform-origin: 116.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eThe second arg is the vector of data.\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: 291.5px 8px; transform-origin: 291.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eThe third and fourth args are the lags and leads that determin the size of the moving window.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function filtered = mopt(func,data,lag,lead)\r\n  filtered = data;\r\nend","test_suite":"%%\r\nx = 1:6;\r\ny = mopt(@mean,x,1,2);\r\ny_correct = [NaN 2.5000 3.5000 4.5000 NaN NaN];\r\nassert(isequal(y(2:end-2),y_correct(2:end-2)) \u0026\u0026 all(isnan(y([1,5,6]))))\r\n\r\n%%\r\nx = [0.2 0.8 0.7 1.1 1.1 1.0 0.2];\r\ny = mopt(@std,x,2,0);\r\ny_correct = [NaN NaN 0.321455025366432 0.208166599946613 0.23094010767585 0.0577350269189626 0.493288286231625];\r\nassert(max(abs(y_correct(3:end)-y(3:end)))\u003c2*eps \u0026\u0026 all(isnan(y(1:2))))\r\n\r\n%%\r\nx = rand(1,10);\r\ny_correct = x;\r\ny = mopt(@mean,x,0,0);\r\nassert(isequal(y,y_correct))\r\n\r\n%%\r\nx = randi(100,1,randi(100));\r\ny_correct = [NaN movsum(x,3,'Endpoints','Discard') NaN];\r\ny = mopt(@sum,x,1,1);\r\nassert(isequaln(y,y_correct))\r\n","published":true,"deleted":false,"likes_count":2,"comments_count":1,"created_by":1939,"edited_by":223089,"edited_at":"2022-12-26T10:37:47.000Z","deleted_by":null,"deleted_at":null,"solvers_count":52,"test_suite_updated_at":"2022-12-26T10:37:47.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2012-02-29T22:04:47.000Z","updated_at":"2025-12-15T21:37:36.000Z","published_at":"2012-02-29T23:39:09.000Z","restored_at":null,"restored_by":null,"spam":null,"simulink":false,"admin_reviewed":false,"description_opc":"{\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eCreate a function that applies an operation (such as @sum, @mean, @std, @norm etc) to a moving window of the data.\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\u003eFirst 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[    filtered = mopt(@mean,1:6,1,2)\\n\\n    Then filtered = [NaN 2.5000 3.5000 4.5000 NaN NaN]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThis is the moving average.\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\u003eSecond 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[    filtered = mopt(@std,[0.2 0.8 0.7 1.1 1.1 1.0 0.2],2,0)\\n\\n    Then filtered = [NaN NaN 0.3215 0.2082 0.2309 0.0577 0.4933]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThis is the 'moving standard error'.\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\u003eThe first arg of mopt is a function handle. It must be a function that takes a vector as input and produces a scalar as output.\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\u003eThe second arg is the vector of data.\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\u003eThe third and fourth args are the lags and leads that determin the size of the moving window.\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":43685,"title":"Apply Function to Each Field of a Structure Array: Part 1","description":"The builtin \u003chttp://www.mathworks.com/help/matlab/ref/structfun.html?=structfun structfun\u003e applies a function handle to each field of a *scalar structure*, but it does not work for structure array. The purpose of this problem is to generalize structfun to enable support for structure array input.\r\n\r\nWrite your own function *structfun2* which accepts a structure array *s* and a function handle *f* as the inputs. The output *c = structfun2(f,s)* is a cell array of the same size as *s*, with each cell storing the result of *f* applied to every field of the relevant structure element in *s*. The structfun2 must preserve the behavior of structfun for scalar structure input. That is, structfun2(f,s) = structfun(f,s) holds for any scalar structure *s*. The assumption required is that *f* is a function that returns a scalar, regardless of the input. \r\n\r\nExample: \r\n\r\n  s = struct('f1',{1, [3 4]; 1, [5 6]},'f2',{[1 2], 2; [3 4 5], [2 5]});\r\n  f = @numel;\r\n  c = {[1;2], [2;1]; [1;3], [2;2]};\r\n\r\nRelated problems in this series:\r\n\r\n* \u003chttp://www.mathworks.com/matlabcentral/cody/problems/43686-apply-function-to-each-field-of-a-structure-array-part-2 Apply Function to Each Field of a Structure Array: Part 2\u003e\r\n* \u003chttp://www.mathworks.com/matlabcentral/cody/problems/43685-apply-function-to-each-field-of-a-structure-array-part-1 Apply Function to Each Field of a Structure Array: Part 1\u003e","description_html":"\u003cp\u003eThe builtin \u003ca href = \"http://www.mathworks.com/help/matlab/ref/structfun.html?=structfun\"\u003estructfun\u003c/a\u003e applies a function handle to each field of a \u003cb\u003escalar structure\u003c/b\u003e, but it does not work for structure array. The purpose of this problem is to generalize structfun to enable support for structure array input.\u003c/p\u003e\u003cp\u003eWrite your own function \u003cb\u003estructfun2\u003c/b\u003e which accepts a structure array \u003cb\u003es\u003c/b\u003e and a function handle \u003cb\u003ef\u003c/b\u003e as the inputs. The output \u003cb\u003ec = structfun2(f,s)\u003c/b\u003e is a cell array of the same size as \u003cb\u003es\u003c/b\u003e, with each cell storing the result of \u003cb\u003ef\u003c/b\u003e applied to every field of the relevant structure element in \u003cb\u003es\u003c/b\u003e. The structfun2 must preserve the behavior of structfun for scalar structure input. That is, structfun2(f,s) = structfun(f,s) holds for any scalar structure \u003cb\u003es\u003c/b\u003e. The assumption required is that \u003cb\u003ef\u003c/b\u003e is a function that returns a scalar, regardless of the input.\u003c/p\u003e\u003cp\u003eExample:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003es = struct('f1',{1, [3 4]; 1, [5 6]},'f2',{[1 2], 2; [3 4 5], [2 5]});\r\nf = @numel;\r\nc = {[1;2], [2;1]; [1;3], [2;2]};\r\n\u003c/pre\u003e\u003cp\u003eRelated problems in this series:\u003c/p\u003e\u003cul\u003e\u003cli\u003e\u003ca href = \"http://www.mathworks.com/matlabcentral/cody/problems/43686-apply-function-to-each-field-of-a-structure-array-part-2\"\u003eApply Function to Each Field of a Structure Array: Part 2\u003c/a\u003e\u003c/li\u003e\u003cli\u003e\u003ca href = \"http://www.mathworks.com/matlabcentral/cody/problems/43685-apply-function-to-each-field-of-a-structure-array-part-1\"\u003eApply Function to Each Field of a Structure Array: Part 1\u003c/a\u003e\u003c/li\u003e\u003c/ul\u003e","function_template":"function y = structfun2(f,s)\r\n  y = x;\r\nend","test_suite":"%%\r\ns.f1 = 'Sunday';\r\ns.f2 = 'Monday';\r\ns.f3 = 'Tuesday'; \r\ns.f4 = 'Wednesday';\r\ns.f5 = 'Thursday';\r\ns.f6 = 'Friday';\r\ns.f7 = 'Saturday';\r\nf = @numel;\r\nc = structfun(@numel, s);\r\nassert(isequal(structfun2(f,s),c))\r\n\r\n%%\r\ns = struct('f1',{1, [3 4]; 1, [5 6]},'f2',{[1 2], 2; [3 4 5], [2 5]});\r\nf = @(x)min(x(:));\r\nc = {[1;1], [3;2]; [1;3], [5;2]};\r\nassert(isequal(structfun2(f,s),c))\r\n\r\n%%\r\ns = struct('f1',{1, [3 4]; 1, [5 6]},'f2',{[1 2], 2; [3 4 5], [2 5]});\r\nf = @(x)max(x(:));\r\nc = {[1;2], [4;2]; [1;5], [6;5]};\r\nassert(isequal(structfun2(f,s),c))\r\n\r\n%%\r\ns = struct('f1',{0, [3 4]; 1, [5 7 6]},'f2',{[0 0], 2; [2 4 5], [0 5]});\r\nf = @nnz;\r\nc = {[0;0], [2;1]; [1;3], [3;1]};\r\nassert(isequal(structfun2(f,s),c))\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":0,"created_by":12569,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":31,"test_suite_updated_at":"2016-12-01T22:39:09.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2016-11-27T01:04:06.000Z","updated_at":"2025-12-14T11:39:11.000Z","published_at":"2016-11-27T01:32:39.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 builtin\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/help/matlab/ref/structfun.html?=structfun\\\"\u003e\u003cw:r\u003e\u003cw:t\u003estructfun\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e applies a function handle to each field of a\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003escalar structure\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, but it does not work for structure array. The purpose of this problem is to generalize structfun to enable support for structure array input.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eWrite your own function\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\u003estructfun2\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e which accepts a structure 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:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003es\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e and a function handle\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\u003ef\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e as the inputs. The output\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\u003ec = structfun2(f,s)\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e is a cell array of the same size as\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\u003es\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, with each cell storing the result of\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ef\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e applied to every field of the relevant structure element 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:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003es\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e. The structfun2 must preserve the behavior of structfun for scalar structure input. That is, structfun2(f,s) = structfun(f,s) holds for any scalar structure\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\u003es\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e. The assumption required is that\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\u003ef\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e is a function that returns a scalar, regardless of the input.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\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[s = struct('f1',{1, [3 4]; 1, [5 6]},'f2',{[1 2], 2; [3 4 5], [2 5]});\\nf = @numel;\\nc = {[1;2], [2;1]; [1;3], [2;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\u003eRelated problems in this series:\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:hyperlink w:docLocation=\\\"http://www.mathworks.com/matlabcentral/cody/problems/43686-apply-function-to-each-field-of-a-structure-array-part-2\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eApply Function to Each Field of a Structure Array: Part 2\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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:hyperlink w:docLocation=\\\"http://www.mathworks.com/matlabcentral/cody/problems/43685-apply-function-to-each-field-of-a-structure-array-part-1\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eApply Function to Each Field of a Structure Array: Part 1\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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":2641,"title":"Dispatch and collect ","description":"Write a function that dispatches the single argument _x_ to multiple function handles (varargin) and concatenates vertically the respective outputs of these functions. All the functions are guaranteed to return the same numbers of outputs of the same size.\r\n\r\nFor example, given\r\n\r\n  x = [1 2 6\r\n       2 7 5 \r\n       3 5 4];\r\n  [bounds, positions] = dispatch(x, @min, @max)\r\n\r\nbounds and position should be:\r\n\r\n  bounds = [1 2 4       %first output of min\r\n            3 7 6]      %first output of max\r\n  positions = [1 1 3    %second output of min \r\n               3 2 1]   %second output of max\r\n\r\n\r\n","description_html":"\u003cp\u003eWrite a function that dispatches the single argument \u003ci\u003ex\u003c/i\u003e to multiple function handles (varargin) and concatenates vertically the respective outputs of these functions. All the functions are guaranteed to return the same numbers of outputs of the same size.\u003c/p\u003e\u003cp\u003eFor example, given\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003ex = [1 2 6\r\n     2 7 5 \r\n     3 5 4];\r\n[bounds, positions] = dispatch(x, @min, @max)\r\n\u003c/pre\u003e\u003cp\u003ebounds and position should be:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003ebounds = [1 2 4       %first output of min\r\n          3 7 6]      %first output of max\r\npositions = [1 1 3    %second output of min \r\n             3 2 1]   %second output of max\r\n\u003c/pre\u003e","function_template":"function varargout = dispatch(x, varargin)\r\n  varargout{:} = [];\r\nend","test_suite":"%% 2 outputs, 2 functions\r\nx = [1 2 6; 2 7 5; 3 5 4];\r\nco1 = [1 2 4; 3 7 6];\r\nco2 = [1 1 3; 3 2 1];\r\n[o1, o2] = dispatch(x, @min, @max);\r\nassert(isequal(o1, co1) \u0026\u0026 isequal(o2, co2))\r\n\r\n%% 1 output, 3 functions\r\nx = randi(50, 20);\r\nco = [mean(x); mode(x); median(x)];\r\nassert(isequal(co, dispatch(x, @mean, @mode, @median)))\r\n\r\n%%  1 output, 5 functions\r\nx=10;\r\nco = [zeros(x);ones(x);eye(x);magic(x);pascal(x)];\r\nassert(isequal(co, dispatch(x, @zeros, @ones, @eye, @magic, @pascal)))\r\n\r\n%% 4 outputs, 1 function\r\nco = randi(50, 1, 4);\r\n[o1, o2, o3, o4] = dispatch(zeros(co), @size);\r\nassert(isequal([o1 o2 o3 o4], co))\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":0,"created_by":999,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":15,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2014-10-23T08:30:27.000Z","updated_at":"2025-09-22T07:23:48.000Z","published_at":"2014-10-23T10:00:26.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\u003eWrite a function that dispatches the single argument\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 to multiple function handles (varargin) and concatenates vertically the respective outputs of these functions. All the functions are guaranteed to return the same numbers of outputs of the same size.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eFor example, given\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 2 6\\n     2 7 5 \\n     3 5 4];\\n[bounds, positions] = dispatch(x, @min, @max)]]\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\u003ebounds and position should 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[bounds = [1 2 4       %first output of min\\n          3 7 6]      %first output of max\\npositions = [1 1 3    %second output of min \\n             3 2 1]   %second output of max]]\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":1210,"title":"Two-output anonymous function?","description":"Return a function handle that when applied to an input, it produces two outputs: the first is the same as the input, and the second is the element-wise square of the input.\r\n\r\nExample:\r\n\r\nf=f2f();\r\n[a b]=f([1 2 3])\r\n\r\na = \r\n1 2 3\r\n\r\nb =\r\n1 4 9\r\n\r\n","description_html":"\u003cp\u003eReturn a function handle that when applied to an input, it produces two outputs: the first is the same as the input, and the second is the element-wise square of the input.\u003c/p\u003e\u003cp\u003eExample:\u003c/p\u003e\u003cp\u003ef=f2f();\r\n[a b]=f([1 2 3])\u003c/p\u003e\u003cp\u003ea = \r\n1 2 3\u003c/p\u003e\u003cp\u003eb =\r\n1 4 9\u003c/p\u003e","function_template":"function y = f2f()\r\n  y = 1;\r\nend","test_suite":"%%\r\nx = [1 2 3];\r\na_correct = x;\r\nb_correct = x.^2;\r\nf=f2f();\r\n[a b]=f(x);\r\nassert(isequal(a,a_correct)\u0026\u0026isequal(b,b_correct))\r\n\r\n%%\r\nx = [-1 2 -3 5 -1];\r\na_correct = x;\r\nb_correct = x.^2;\r\nf=f2f();\r\n[a b]=f(x);\r\nassert(isequal(a,a_correct)\u0026\u0026isequal(b,b_correct))\r\n","published":true,"deleted":false,"likes_count":4,"comments_count":2,"created_by":3399,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":84,"test_suite_updated_at":null,"rescore_all_solutions":true,"group_id":1,"created_at":"2013-01-17T17:17:14.000Z","updated_at":"2025-12-10T11:16:02.000Z","published_at":"2013-01-17T17:19: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\",\"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\u003eReturn a function handle that when applied to an input, it produces two outputs: the first is the same as the input, and the second is the element-wise square of the input.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\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\u003ef=f2f(); [a b]=f([1 2 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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ea = 1 2 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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eb = 1 4 9\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":582,"title":"Function composition - harder","description":"Write a function that accepts an arbitrary number of function handles f_1, f_2, ..., f_n and returns the composition h. That is,\r\n\r\nh = (f_1 o f_2 o ... o f_n)(x) = f_1(f_2( ... f_n(x) ... ))\r\n\r\nExample:\r\n\r\n  \u003e\u003e f1 = @(x)x+1;\r\n  \u003e\u003e f2 = @(x)3*x;\r\n  \u003e\u003e f3 = @sqrt;\r\n  \u003e\u003e h = composeFcn(f1,f2,f3);\r\n  \u003e\u003e h(9)\r\n  ans =\r\n    10\r\n    \r\nbecause 3*sqrt(9)+1 = 10.\r\n\r\nYou can assume that there will always be at least one input passed to the *composeFcn* function.","description_html":"\u003cp\u003eWrite a function that accepts an arbitrary number of function handles f_1, f_2, ..., f_n and returns the composition h. That is,\u003c/p\u003e\u003cp\u003eh = (f_1 o f_2 o ... o f_n)(x) = f_1(f_2( ... f_n(x) ... ))\u003c/p\u003e\u003cp\u003eExample:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e\u0026gt;\u0026gt; f1 = @(x)x+1;\r\n\u0026gt;\u0026gt; f2 = @(x)3*x;\r\n\u0026gt;\u0026gt; f3 = @sqrt;\r\n\u0026gt;\u0026gt; h = composeFcn(f1,f2,f3);\r\n\u0026gt;\u0026gt; h(9)\r\nans =\r\n  10\r\n\u003c/pre\u003e\u003cp\u003ebecause 3*sqrt(9)+1 = 10.\u003c/p\u003e\u003cp\u003eYou can assume that there will always be at least one input passed to the \u003cb\u003ecomposeFcn\u003c/b\u003e function.\u003c/p\u003e","function_template":"function h = composeFcn(varargin)\r\nend","test_suite":"%%\r\nf1 = @(x)x+1;\r\nf2 = @(x)3*x;\r\nf3 = @sqrt;\r\nh = composeFcn(f1,f2,f3);\r\nassert(isequal(h(9),10));\r\n\r\n%%\r\nf = repmat({@(x)x+1},1,100);\r\nh = composeFcn(f{:});\r\nassert(isequal(h(0),100));\r\n\r\n%%\r\nf = @(x)x;\r\nh = composeFcn(f);\r\nassert(isequal(h(1234),1234));","published":true,"deleted":false,"likes_count":6,"comments_count":2,"created_by":19,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":106,"test_suite_updated_at":"2017-03-03T15:36:10.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2012-04-13T18:58:27.000Z","updated_at":"2025-12-05T11:00:12.000Z","published_at":"2012-04-13T19:27:40.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\u003eWrite a function that accepts an arbitrary number of function handles f_1, f_2, ..., f_n and returns the composition h. That is,\u003c/w:t\u003e\u003c/w:r\u003e\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\u003eh = (f_1 o f_2 o ... o f_n)(x) = f_1(f_2( ... f_n(x) ... ))\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExample:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[\u003e\u003e f1 = @(x)x+1;\\n\u003e\u003e f2 = @(x)3*x;\\n\u003e\u003e f3 = @sqrt;\\n\u003e\u003e h = composeFcn(f1,f2,f3);\\n\u003e\u003e h(9)\\nans =\\n  10]]\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\u003ebecause 3*sqrt(9)+1 = 10.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYou can assume that there will always be at least one input passed to the\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ecomposeFcn\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e function.\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":43686,"title":"Apply Function to Each Field of a Structure Array: Part 2","description":"The builtin \u003chttp://www.mathworks.com/help/matlab/ref/structfun.html?=structfun structfun\u003e applies a function handle to each field of a *scalar structure*, but it does not work for structure array. The purpose of this problem is to generalize structfun to enable support for structure array input.\r\n\r\nWrite your own function *structfun2* which accepts a structure array *s* and a function handle *f* as the inputs. The output *c = structfun2(f,s)* is a structure array *c* of the same size and same fields as *s*, where each field of the structure element of c stores the result of *f* applied to the same field of the relevant structure element in *s*. The structfun2 must preserve the behavior of structfun for scalar structure input. That is, when *s* is a scalar structure, your output reduces to c = structfun(f,s,'UniformOutput',false). No assumption on *f* is needed in this problem. \r\n \r\n\r\nExample: \r\n\r\n  s = struct('f1',{1, [3 4]; 1, [5 6]},'f2',{[1 2], 2; [3 4 5], [2 5]});\r\n  f = @numel;\r\n  c = struct('f1',{1, 2; 1, 2},'f2',{2, 1; 3, 2});\r\n\r\nRelated problems in this series:\r\n\r\n* \u003chttp://www.mathworks.com/matlabcentral/cody/problems/43685-apply-function-to-each-field-of-a-structure-array-part-1 Apply Function to Each Field of a Structure Array: Part 1\u003e\r\n* \u003chttp://www.mathworks.com/matlabcentral/cody/problems/43686-apply-function-to-each-field-of-a-structure-array-part-2 Apply Function to Each Field of a Structure Array: Part 2\u003e\r\n","description_html":"\u003cp\u003eThe builtin \u003ca href = \"http://www.mathworks.com/help/matlab/ref/structfun.html?=structfun\"\u003estructfun\u003c/a\u003e applies a function handle to each field of a \u003cb\u003escalar structure\u003c/b\u003e, but it does not work for structure array. The purpose of this problem is to generalize structfun to enable support for structure array input.\u003c/p\u003e\u003cp\u003eWrite your own function \u003cb\u003estructfun2\u003c/b\u003e which accepts a structure array \u003cb\u003es\u003c/b\u003e and a function handle \u003cb\u003ef\u003c/b\u003e as the inputs. The output \u003cb\u003ec = structfun2(f,s)\u003c/b\u003e is a structure array \u003cb\u003ec\u003c/b\u003e of the same size and same fields as \u003cb\u003es\u003c/b\u003e, where each field of the structure element of c stores the result of \u003cb\u003ef\u003c/b\u003e applied to the same field of the relevant structure element in \u003cb\u003es\u003c/b\u003e. The structfun2 must preserve the behavior of structfun for scalar structure input. That is, when \u003cb\u003es\u003c/b\u003e is a scalar structure, your output reduces to c = structfun(f,s,'UniformOutput',false). No assumption on \u003cb\u003ef\u003c/b\u003e is needed in this problem.\u003c/p\u003e\u003cp\u003eExample:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003es = struct('f1',{1, [3 4]; 1, [5 6]},'f2',{[1 2], 2; [3 4 5], [2 5]});\r\nf = @numel;\r\nc = struct('f1',{1, 2; 1, 2},'f2',{2, 1; 3, 2});\r\n\u003c/pre\u003e\u003cp\u003eRelated problems in this series:\u003c/p\u003e\u003cul\u003e\u003cli\u003e\u003ca href = \"http://www.mathworks.com/matlabcentral/cody/problems/43685-apply-function-to-each-field-of-a-structure-array-part-1\"\u003eApply Function to Each Field of a Structure Array: Part 1\u003c/a\u003e\u003c/li\u003e\u003cli\u003e\u003ca href = \"http://www.mathworks.com/matlabcentral/cody/problems/43686-apply-function-to-each-field-of-a-structure-array-part-2\"\u003eApply Function to Each Field of a Structure Array: Part 2\u003c/a\u003e\u003c/li\u003e\u003c/ul\u003e","function_template":"function y = structfun2(f,s)\r\n  y = s;\r\nend","test_suite":"%%\r\ns.f1 = 'Sunday';\r\ns.f2 = 'Monday';\r\ns.f3 = 'Tuesday'; \r\ns.f4 = 'Wednesday';\r\ns.f5 = 'Thursday';\r\ns.f6 = 'Friday';\r\ns.f7 = 'Saturday';\r\nf = @(x)x(1:3);\r\nc = structfun(f,s,'UniformOutput',false);\r\nassert(isequal(structfun2(f,s),c))\r\n\r\n%%\r\ns = struct('f1',{1, [3 4;5 6]; 1, [5 6;7 8]},'f2',{[1 2], 2; [3 4 5;6 7 8], [2 6;4 5]});\r\nf = @min;\r\nc = struct('f1',{1, [3 4]; 1, [5 6]},'f2',{1, 2; [3 4 5], [2 5]});\r\nassert(isequal(structfun2(f,s),c))\r\n\r\n%%\r\ns = struct('f1',{1, [3 4;5 6]; 1, [5 6;7 8]},'f2',{[1 2], 2; [3 4 5;6 7 8], [2 6;4 5]});\r\nf = @sum;\r\nc = struct('f1',{1, [8 10]; 1, [12 14]},'f2',{3, 2; [9 11 13], [6 11]});\r\nassert(isequal(structfun2(f,s),c))\r\n\r\n%%\r\ns = struct('f1',{1, [3 4;5 6]; 1, [5 6;7 8]},'f2',{[1 2], 2; [3 4 5;6 7 8], [2 6;4 5]});\r\nf = @pow2;\r\nc = struct('f1',{2, [8 16;32 64]; 2, [32 64;128 256]},'f2',{[2 4], 4; [8 16 32;64 128 256], [4 64;16 32]});\r\nassert(isequal(structfun2(f,s),c))\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":1,"created_by":12569,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":24,"test_suite_updated_at":"2016-12-01T22:27:07.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2016-11-27T02:12:26.000Z","updated_at":"2025-12-15T21:01:07.000Z","published_at":"2016-11-27T02:12: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\u003eThe builtin\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/help/matlab/ref/structfun.html?=structfun\\\"\u003e\u003cw:r\u003e\u003cw:t\u003estructfun\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e applies a function handle to each field of a\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003escalar structure\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, but it does not work for structure array. The purpose of this problem is to generalize structfun to enable support for structure array input.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eWrite your own function\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\u003estructfun2\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e which accepts a structure 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:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003es\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e and a function handle\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\u003ef\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e as the inputs. The output\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\u003ec = structfun2(f,s)\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e is a structure 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:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ec\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e of the same size and same fields as\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\u003es\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, where each field of the structure element of c stores the result of\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ef\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e applied to the same field of the relevant structure element 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:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003es\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e. The structfun2 must preserve the behavior of structfun for scalar structure input. That is, when\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\u003es\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e is a scalar structure, your output reduces to c = structfun(f,s,'UniformOutput',false). No assumption on\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\u003ef\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e is needed in this problem.\u003c/w:t\u003e\u003c/w:r\u003e\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[s = struct('f1',{1, [3 4]; 1, [5 6]},'f2',{[1 2], 2; [3 4 5], [2 5]});\\nf = @numel;\\nc = struct('f1',{1, 2; 1, 2},'f2',{2, 1; 3, 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\u003eRelated problems in this series:\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:hyperlink w:docLocation=\\\"http://www.mathworks.com/matlabcentral/cody/problems/43685-apply-function-to-each-field-of-a-structure-array-part-1\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eApply Function to Each Field of a Structure Array: Part 1\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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:hyperlink w:docLocation=\\\"http://www.mathworks.com/matlabcentral/cody/problems/43686-apply-function-to-each-field-of-a-structure-array-part-2\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eApply Function to Each Field of a Structure Array: Part 2\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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":1198,"title":"Handle to an array of functions","description":"Given a cell array of functions that operate on scalars, it is required to return a function handle to process a vector of values applying the functions successively (the first function operates on x, the second function operates on the resulting output, etc.).\r\n\r\nExample:\r\n\r\n \u003e\u003e f{1}=@(x) x^2;\r\n\r\n \u003e\u003e f{2}=@(x) x+3;\r\n\r\n \u003e\u003e f{3}=@(x) x/2;\r\n\r\n \u003e\u003e g=cf(f);\r\n\r\n \u003e\u003e x=[1 2 3];\r\n\r\n \u003e\u003e g(x)\r\n\r\n ans =\r\n\r\n     2.0000    3.5000    6.0000\r\n ","description_html":"\u003cp\u003eGiven a cell array of functions that operate on scalars, it is required to return a function handle to process a vector of values applying the functions successively (the first function operates on x, the second function operates on the resulting output, etc.).\u003c/p\u003e\u003cp\u003eExample:\u003c/p\u003e\u003cpre\u003e \u003e\u003e f{1}=@(x) x^2;\u003c/pre\u003e\u003cpre\u003e \u003e\u003e f{2}=@(x) x+3;\u003c/pre\u003e\u003cpre\u003e \u003e\u003e f{3}=@(x) x/2;\u003c/pre\u003e\u003cpre\u003e \u003e\u003e g=cf(f);\u003c/pre\u003e\u003cpre\u003e \u003e\u003e x=[1 2 3];\u003c/pre\u003e\u003cpre\u003e \u003e\u003e g(x)\u003c/pre\u003e\u003cpre\u003e ans =\u003c/pre\u003e\u003cpre\u003e     2.0000    3.5000    6.0000\u003c/pre\u003e","function_template":"function y = cf(x)\r\n  y = x;\r\nend","test_suite":"%%\r\nf{1}=@(x) x^2;\r\nf{2}=@(x) x+3;\r\nf{3}=@(x) x/2;\r\ng=cf(f);\r\nx=[1 2 3];\r\ny_correct = [2 3.5 6];\r\nassert(isequal(g(x),y_correct))\r\n%%\r\nf{1}=@(x) x^0.5;\r\nf{2}=@(x) x-1;\r\nf{3}=@(x) x^2;\r\nf{4}=@(x) x/3;\r\ng=cf(f);\r\nx=[ 16 49 100];\r\ny_correct = [3 12 27];\r\nassert(isequal(g(x),y_correct))\r\n","published":true,"deleted":false,"likes_count":6,"comments_count":1,"created_by":3399,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":61,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2013-01-11T18:06:01.000Z","updated_at":"2025-12-10T11:02:26.000Z","published_at":"2013-01-11T18:06:25.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eGiven a cell array of functions that operate on scalars, it is required to return a function handle to process a vector of values applying the functions successively (the first function operates on x, the second function operates on the resulting output, etc.).\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExample:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ \u003e\u003e f{1}=@(x) x^2;\\n\\n \u003e\u003e f{2}=@(x) x+3;\\n\\n \u003e\u003e f{3}=@(x) x/2;\\n\\n \u003e\u003e g=cf(f);\\n\\n \u003e\u003e x=[1 2 3];\\n\\n \u003e\u003e g(x)\\n\\n ans =\\n\\n     2.0000    3.5000    6.0000]]\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":44808,"title":"Make an anonymous function that has variable output","description":"Make a anonymous function that has variable output.\r\n\r\n\r\nf = @(x)...\r\n\r\nthe following equation→equation(s) as followed has(ve)\r\n\r\na = f(x) ==\u003e a = x{1}\r\n\r\n[a,b] = f(x) ==\u003e a = x{1}, b = x{2}\r\n\r\n...\r\n\r\nYou can see the test suite for details.\r\n","description_html":"\u003cp\u003eMake a anonymous function that has variable output.\u003c/p\u003e\u003cp\u003ef = @(x)...\u003c/p\u003e\u003cp\u003ethe following equation→equation(s) as followed has(ve)\u003c/p\u003e\u003cp\u003ea = f(x) ==\u0026gt; a = x{1}\u003c/p\u003e\u003cp\u003e[a,b] = f(x) ==\u0026gt; a = x{1}, b = x{2}\u003c/p\u003e\u003cp\u003e...\u003c/p\u003e\u003cp\u003eYou can see the test suite for details.\u003c/p\u003e","function_template":"function y = anonymous(x)\r\n  y = x;\r\nend","test_suite":"%% 1\r\nfid = fopen('anonymous.m');\r\nst = regexprep(char(fread(fid)'), 'function', 'error(''No fancy functions!''); %','ignorecase',2);\r\nfclose(fid);\r\nfid = fopen('anonymous.m' , 'w');\r\nfwrite(fid,st);\r\nfclose(fid);\r\n%% 2\r\nf = anonymous;\r\na = f(1);\r\nassert(a==1);\r\n%% 3\r\nf = anonymous;\r\na = f(1,2);\r\nassert(a==1);\r\n[a,b] = f(3,5);\r\nassert(a==3\u0026b==5);\r\n%% 4\r\nf = anonymous;\r\na = f(1,2);\r\nassert(a==1);\r\n[a,b] = f(3,'56');\r\nassert(a==3\u0026isequal(b,'56'));\r\n\r\n\r\n","published":true,"deleted":false,"likes_count":2,"comments_count":1,"created_by":3668,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":31,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2018-12-02T13:36:44.000Z","updated_at":"2025-12-14T20:24:59.000Z","published_at":"2018-12-02T13:36:49.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\u003eMake a anonymous function that has variable output.\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\u003ef = @(x)...\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\u003ethe following equation→equation(s) as followed has(ve)\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\u003ea = f(x) ==\u0026gt; a = x{1}\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e[a,b] = f(x) ==\u0026gt; a = x{1}, b = x{2}\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\u003e...\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYou can see the test suite for details.\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":583,"title":"Implement a counter","description":"Write a function that returns a function that counts the number of times it is invoked. Example:\r\n\r\n  \u003e\u003e h = counter;\r\n  \u003e\u003e h()\r\n  ans =\r\n       1\r\n  \u003e\u003e h()\r\n  ans =\r\n       2\r\n  \u003e\u003e h()\r\n  ans =\r\n       3\r\n\r\nNote: your solution *cannot* use persistent variables.","description_html":"\u003cp\u003eWrite a function that returns a function that counts the number of times it is invoked. Example:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e\u003e\u003e h = counter;\r\n\u003e\u003e h()\r\nans =\r\n     1\r\n\u003e\u003e h()\r\nans =\r\n     2\r\n\u003e\u003e h()\r\nans =\r\n     3\r\n\u003c/pre\u003e\u003cp\u003eNote: your solution \u003cb\u003ecannot\u003c/b\u003e use persistent variables.\u003c/p\u003e","function_template":"function h = counter;\r\nend","test_suite":"%%\r\nh = counter;\r\nassert(isequal(h(), 1))\r\nassert(isequal(h(), 2))\r\nassert(isequal(h(), 3))\r\nassert(isequal(h(), 4))\r\nassert(isequal(h(), 5))\r\n\r\n%%\r\ncode = fileread('counter.m');\r\nassert(isempty(strfind(code, 'persistent')));","published":true,"deleted":false,"likes_count":6,"comments_count":0,"created_by":19,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":102,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2012-04-13T18:59:04.000Z","updated_at":"2025-12-11T12:06:28.000Z","published_at":"2012-04-13T19:27:45.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\u003eWrite a function that returns a function that counts the number of times it is invoked. 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[\u003e\u003e h = counter;\\n\u003e\u003e h()\\nans =\\n     1\\n\u003e\u003e h()\\nans =\\n     2\\n\u003e\u003e h()\\nans =\\n     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\u003eNote: your solution\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\u003ecannot\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e use persistent variables.\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":1224,"title":"Flexible Anonymous Function","description":"Given a function handle, return a handle to a function that would accept an arbitrary number of inputs, applies the function to each input, and returns an arbitrary number of (respective) outputs (\u003c= the number of inputs, of course).\r\n\r\nExample:\r\n\r\n myf=@(x) det(x);\r\n\r\n yourf = flexf(myf);\r\n\r\n [a,~,b,c] = yourf([1 2;3 4],7,[1 2 3; 4 5 6; 2 3 1],3,[2 -1 ; 1 -1])\r\n\r\n a = -2\r\n b = 9\r\n c = 3 \r\n\r\n","description_html":"\u003cp\u003eGiven a function handle, return a handle to a function that would accept an arbitrary number of inputs, applies the function to each input, and returns an arbitrary number of (respective) outputs (\u0026lt;= the number of inputs, of course).\u003c/p\u003e\u003cp\u003eExample:\u003c/p\u003e\u003cpre\u003e myf=@(x) det(x);\u003c/pre\u003e\u003cpre\u003e yourf = flexf(myf);\u003c/pre\u003e\u003cpre\u003e [a,~,b,c] = yourf([1 2;3 4],7,[1 2 3; 4 5 6; 2 3 1],3,[2 -1 ; 1 -1])\u003c/pre\u003e\u003cpre\u003e a = -2\r\n b = 9\r\n c = 3 \u003c/pre\u003e","function_template":"function y = flexf(f)\r\n  y = f;\r\nend","test_suite":"%%\r\nmyf=@(x) det(x);\r\nyourf = flexf(myf);\r\nx={[1 2;3 4],7,[1 2 3; 4 5 6; 2 3 1],3,[2 -1 ; 1 -1]};\r\n[a,~,b,c]=yourf(x{:});\r\ny_correct = {-2 9 3};\r\nassert(isequal({a b c},y_correct));\r\n%%\r\nmyf=@(x) x.^2;\r\nyourf = flexf(myf);\r\nx={[1 2;3 4],7,[1 2 3; 4 5 6; 2 3 1],3,[2 -1 ; 1 -1]};\r\n[a,~,b,c]=yourf(x{:});\r\ny_correct = cellfun(myf,x,'uni',0);\r\nassert(isequal({a b c},y_correct([1 3 4])));\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":0,"created_by":3399,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":35,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2013-01-23T21:46:41.000Z","updated_at":"2025-12-15T22:03:18.000Z","published_at":"2013-01-23T22:04:52.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 function handle, return a handle to a function that would accept an arbitrary number of inputs, applies the function to each input, and returns an arbitrary number of (respective) outputs (\u0026lt;= the number of inputs, of course).\u003c/w:t\u003e\u003c/w:r\u003e\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[ myf=@(x) det(x);\\n\\n yourf = flexf(myf);\\n\\n [a,~,b,c] = yourf([1 2;3 4],7,[1 2 3; 4 5 6; 2 3 1],3,[2 -1 ; 1 -1])\\n\\n a = -2\\n b = 9\\n c = 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":24,"title":"Function Iterator","description":"Given a handle fh to a function which takes a scalar input and returns a scalar output and an integer n \u003e= 1, return a  handle fh2 to a function which applies the given function n times.\n\nExamples:\n\n \u003e\u003e addOne = @(x)x+1;\n \u003e\u003e addTen = iterate_fcn(addOne, 10);\n \u003e\u003e addTen(3)\n ans =\n     13\n\n \u003e\u003e squarer = @(a) a^2;\n \u003e\u003e fh2 = iterate_fcn(squarer, 3);\n \u003e\u003e fh2(3)\n ans =\n         6561\n\n % Golden Ratio\n \u003e\u003e fh = @(y)sqrt(y+1);\n \u003e\u003e fh2 = iterate_fcn(fh,30);\n \u003e\u003e fh2(1)\n ans =\n     1.6180\n","description_html":"\u003cp\u003eGiven a handle fh to a function which takes a scalar input and returns a scalar output and an integer n \u003e= 1, return a  handle fh2 to a function which applies the given function n times.\u003c/p\u003e\u003cp\u003eExamples:\u003c/p\u003e\u003cpre\u003e \u003e\u003e addOne = @(x)x+1;\n \u003e\u003e addTen = iterate_fcn(addOne, 10);\n \u003e\u003e addTen(3)\n ans =\n     13\u003c/pre\u003e\u003cpre\u003e \u003e\u003e squarer = @(a) a^2;\n \u003e\u003e fh2 = iterate_fcn(squarer, 3);\n \u003e\u003e fh2(3)\n ans =\n         6561\u003c/pre\u003e\u003cpre\u003e % Golden Ratio\n \u003e\u003e fh = @(y)sqrt(y+1);\n \u003e\u003e fh2 = iterate_fcn(fh,30);\n \u003e\u003e fh2(1)\n ans =\n     1.6180\u003c/pre\u003e","function_template":"function fh2 = iterate_fcn(fh, n)\nfh2 = fh;\nend","test_suite":"%%\nnoOp = @(x)x;\nfh2 = iterate_fcn(noOp, 50);\nassert(isequal(fh2(pi),pi));\n\n\n%%\naddOne = @(x)x+1;\naddTen = iterate_fcn(addOne, 10);\nassert(isequal(addTen(3),13));\n\n%%\naddOne = @(x)x+1;\naddOne2 = iterate_fcn(addOne, 1);\nassert(isequal(addOne2(3),4));\n\n%%\nsquarer = @(a) a^2;\nfh2 = iterate_fcn(squarer, 3);\nassert(isequal(fh2(3),6561));\n\n%%\nfh = @(y)sqrt(y+1);\nfh2 = iterate_fcn(fh,30);\nassert(abs(fh2(1) - (1+sqrt(5))/2) \u003c 100*eps);","published":true,"deleted":false,"likes_count":61,"comments_count":27,"created_by":1,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":2244,"test_suite_updated_at":"2012-01-18T01:00:20.000Z","rescore_all_solutions":false,"group_id":2,"created_at":"2012-01-18T01:00:20.000Z","updated_at":"2026-03-15T20:56:03.000Z","published_at":"2012-01-18T01:00:20.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eGiven a handle fh to a function which takes a scalar input and returns a scalar output and an integer n \u0026gt;= 1, return a handle fh2 to a function which applies the given function n times.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExamples:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ \u003e\u003e addOne = @(x)x+1;\\n \u003e\u003e addTen = iterate_fcn(addOne, 10);\\n \u003e\u003e addTen(3)\\n ans =\\n     13\\n\\n \u003e\u003e squarer = @(a) a^2;\\n \u003e\u003e fh2 = iterate_fcn(squarer, 3);\\n \u003e\u003e fh2(3)\\n ans =\\n         6561\\n\\n % Golden Ratio\\n \u003e\u003e fh = @(y)sqrt(y+1);\\n \u003e\u003e fh2 = iterate_fcn(fh,30);\\n \u003e\u003e fh2(1)\\n ans =\\n     1.6180]]\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":733,"title":"Extract Built In Functions and Toolbox Functions from String or Function Handle","description":"Find the Built-In functions and Toolbox functions in either a string or a function handle.\r\n\r\nGenerate a string of alphabetized Built-In functions followed by alphabetized Functions.\r\n\r\n*Inputs:*\r\n\r\nfh=@(x)log10(x)+log2(x)+abs(x)\r\n\r\nstr='smooth3(x,y)-filter(x)+abs(n)+filter2(u)+sin(x)+numel(z)'\r\n\r\n*Outputs:*\r\n\r\n'abs log2 log10'\r\n\r\n'abs filter numel sin filter2 smooth3'\r\n\r\nRelated to \r\n\u003chttp://www.mathworks.com/matlabcentral/cody/problems/464-function-sniffer Cody_464\u003e","description_html":"\u003cp\u003eFind the Built-In functions and Toolbox functions in either a string or a function handle.\u003c/p\u003e\u003cp\u003eGenerate a string of alphabetized Built-In functions followed by alphabetized Functions.\u003c/p\u003e\u003cp\u003e\u003cb\u003eInputs:\u003c/b\u003e\u003c/p\u003e\u003cp\u003efh=@(x)log10(x)+log2(x)+abs(x)\u003c/p\u003e\u003cp\u003estr='smooth3(x,y)-filter(x)+abs(n)+filter2(u)+sin(x)+numel(z)'\u003c/p\u003e\u003cp\u003e\u003cb\u003eOutputs:\u003c/b\u003e\u003c/p\u003e\u003cp\u003e'abs log2 log10'\u003c/p\u003e\u003cp\u003e'abs filter numel sin filter2 smooth3'\u003c/p\u003e\u003cp\u003eRelated to  \u003ca href=\"http://www.mathworks.com/matlabcentral/cody/problems/464-function-sniffer\"\u003eCody_464\u003c/a\u003e\u003c/p\u003e","function_template":"function functions = find_functions(fh_str)\r\n  functions = '';\r\nend","test_suite":"%%\r\nfh_str='log2(x)+smooth3(x,y)+abs(2)+log10(5)';\r\nexp_str='abs log10 log2 smooth3';\r\nassert(isequal(find_functions(fh_str),exp_str))\r\n%%\r\nfh_str='for k=log10(x):log2(x)+abs(x)';\r\nexp_str='abs for log10 log2';\r\nassert(isequal(find_functions(fh_str),exp_str))\r\n%%\r\nfh_str=@(x)x^2+sin(x)-cos(x);\r\nexp_str='cos sin';\r\nassert(isequal(find_functions(fh_str),exp_str))\r\n%%\r\nfh_str='@(x)x^2+sin(x)-cos(x)';\r\nexp_str='cos sin';\r\nassert(isequal(find_functions(fh_str),exp_str))\r\n%%\r\nfh_str='filter2(x,A)+filter(x)-cos(x) expm(z)';\r\nexp_str='cos filter expm filter2';\r\nassert(isequal(find_functions(fh_str),exp_str))\r\n%%\r\nfh_str='smooth3(x,y)-filter(x)+abs(n)+filter2(u)+sin(x)+numel(z)';\r\nexp_str='abs filter numel sin filter2 smooth3';\r\nassert(isequal(find_functions(fh_str),exp_str))\r\n","published":true,"deleted":false,"likes_count":5,"comments_count":6,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":83,"test_suite_updated_at":"2012-07-18T13:18:16.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2012-06-01T23:09:01.000Z","updated_at":"2026-03-31T20:12:36.000Z","published_at":"2012-06-02T00:17:41.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eFind the Built-In functions and Toolbox functions in either a string or a function handle.\u003c/w:t\u003e\u003c/w:r\u003e\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\u003eGenerate a string of alphabetized Built-In functions followed by alphabetized Functions.\u003c/w:t\u003e\u003c/w:r\u003e\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\u003eInputs:\u003c/w:t\u003e\u003c/w:r\u003e\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\u003efh=@(x)log10(x)+log2(x)+abs(x)\u003c/w:t\u003e\u003c/w:r\u003e\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\u003estr='smooth3(x,y)-filter(x)+abs(n)+filter2(u)+sin(x)+numel(z)'\u003c/w:t\u003e\u003c/w:r\u003e\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\u003eOutputs:\u003c/w:t\u003e\u003c/w:r\u003e\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'abs log2 log10'\u003c/w:t\u003e\u003c/w:r\u003e\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'abs filter numel sin filter2 smooth3'\u003c/w:t\u003e\u003c/w:r\u003e\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\u003eRelated to \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/464-function-sniffer\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eCody_464\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"}],"term":"group:\"Handling Functions\"","current_player_id":null,"fields":[{"name":"page","type":"integer","callback":null,"default":1,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"per_page","type":"integer","callback":null,"default":50,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"sort","type":"string","callback":null,"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"body","type":"text","callback":null,"default":"*:*","directive":null,"facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":false},{"name":"group","type":"string","callback":null,"default":null,"directive":"group","facet":true,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"difficulty_rating_bin","type":"string","callback":null,"default":null,"directive":"difficulty_rating_bin","facet":true,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"id","type":"integer","callback":null,"default":null,"directive":"id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"tag","type":"string","callback":null,"default":null,"directive":"tag","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"product","type":"string","callback":null,"default":null,"directive":"product","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"created_at","type":"timeframe","callback":{},"default":null,"directive":"created_at","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"profile_id","type":"integer","callback":null,"default":null,"directive":"author_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"created_by","type":"string","callback":null,"default":null,"directive":"author","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"player_id","type":"integer","callback":null,"default":null,"directive":"solver_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"player","type":"string","callback":null,"default":null,"directive":"solver","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"solvers_count","type":"integer","callback":null,"default":null,"directive":"solvers_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"comments_count","type":"integer","callback":null,"default":null,"directive":"comments_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"likes_count","type":"integer","callback":null,"default":null,"directive":"likes_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"leader_id","type":"integer","callback":null,"default":null,"directive":"leader_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"leading_solution","type":"integer","callback":null,"default":null,"directive":"leading_solution","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true}],"filters":[{"name":"asset_type","type":"string","callback":null,"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":"\"cody:problem\"","prepend":true},{"name":"profile_id","type":"integer","callback":{},"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":"author_id","static":null,"prepend":true}],"query":{"params":{"per_page":50,"term":"group:\"Handling Functions\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"group":[["group:\"Handling Functions\"","","\"","Handling Functions","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007fdbe9e08b80\u003e":["Handling Functions"],"#\u003cMathWorks::Search::Field:0x00007fdbe9e07c80\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007fdbe9e05f20\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007fdbe9e09440\u003e":1,"#\u003cMathWorks::Search::Field:0x00007fdbe9e093a0\u003e":50,"#\u003cMathWorks::Search::Field:0x00007fdbe9e09300\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007fdbe9e09080\u003e":"group:\"Handling Functions\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007fdbe9e09080\u003e":"group:\"Handling Functions\""},"queried_facets":{"#\u003cMathWorks::Search::Field:0x00007fdbe9e08b80\u003e":["Handling Functions"]}},"query_backend":{"connection":{"configuration":{"index_url":"http://index-op-v2/solr/","query_url":"http://search-op-v2/solr/","direct_access_index_urls":["http://index-op-v2/solr/"],"direct_access_query_urls":["http://search-op-v2/solr/"],"timeout":10,"vhost":"search","exchange":"search.topic","heartbeat":30,"pre_index_mode":false,"host":"rabbitmq-eks","port":5672,"username":"cody-search","password":"78X075ddcV44","virtual_host":"search","indexer":"amqp","http_logging":"true","core":"cody"},"query_connection":{"uri":"http://search-op-v2/solr/cody/","proxy":null,"connection":{"parallel_manager":null,"headers":{"User-Agent":"Faraday v1.0.1"},"params":{},"options":{"params_encoder":"Faraday::FlatParamsEncoder","proxy":null,"bind":null,"timeout":null,"open_timeout":null,"read_timeout":null,"write_timeout":null,"boundary":null,"oauth":null,"context":null,"on_data":null},"ssl":{"verify":true,"ca_file":null,"ca_path":null,"verify_mode":null,"cert_store":null,"client_cert":null,"client_key":null,"certificate":null,"private_key":null,"verify_depth":null,"version":null,"min_version":null,"max_version":null},"default_parallel_manager":null,"builder":{"adapter":{"name":"Faraday::Adapter::NetHttp","args":[],"block":null},"handlers":[{"name":"Faraday::Response::RaiseError","args":[],"block":null}],"app":{"app":{"ssl_cert_store":{"verify_callback":null,"error":null,"error_string":null,"chain":null,"time":null},"app":{},"connection_options":{},"config_block":null}}},"url_prefix":"http://search-op-v2/solr/cody/","manual_proxy":false,"proxy":null},"update_format":"RSolr::JSON::Generator","update_path":"update","options":{"url":"http://search-op-v2/solr/cody"}}},"query":{"params":{"per_page":50,"term":"group:\"Handling Functions\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"group":[["group:\"Handling Functions\"","","\"","Handling Functions","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007fdbe9e08b80\u003e":["Handling Functions"],"#\u003cMathWorks::Search::Field:0x00007fdbe9e07c80\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007fdbe9e05f20\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007fdbe9e09440\u003e":1,"#\u003cMathWorks::Search::Field:0x00007fdbe9e093a0\u003e":50,"#\u003cMathWorks::Search::Field:0x00007fdbe9e09300\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007fdbe9e09080\u003e":"group:\"Handling Functions\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007fdbe9e09080\u003e":"group:\"Handling Functions\""},"queried_facets":{"#\u003cMathWorks::Search::Field:0x00007fdbe9e08b80\u003e":["Handling Functions"]}},"options":{"fields":["id","difficulty_rating"]},"join":" "},"results":[{"id":42447,"difficulty_rating":"easy"},{"id":581,"difficulty_rating":"easy"},{"id":44083,"difficulty_rating":"easy-medium"},{"id":260,"difficulty_rating":"easy-medium"},{"id":1196,"difficulty_rating":"easy-medium"},{"id":429,"difficulty_rating":"easy-medium"},{"id":43685,"difficulty_rating":"easy-medium"},{"id":2641,"difficulty_rating":"easy-medium"},{"id":1210,"difficulty_rating":"easy-medium"},{"id":582,"difficulty_rating":"easy-medium"},{"id":43686,"difficulty_rating":"easy-medium"},{"id":1198,"difficulty_rating":"medium"},{"id":44808,"difficulty_rating":"medium"},{"id":583,"difficulty_rating":"medium"},{"id":1224,"difficulty_rating":"medium"},{"id":24,"difficulty_rating":"medium"},{"id":733,"difficulty_rating":"medium-hard"}]}}