{"group":{"id":1,"name":"Community","lockable":false,"created_at":"2012-01-18T18:02:15.000Z","updated_at":"2026-05-26T00:16:20.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-05-26T00: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":970,"title":"Piecewise linear interpolation","description":"Given an Mx2 vector and a row of M-1 integers, output a two column vector that linearly interpolates Y times between each successive pair of values in X.  Any consecutive duplicate points are to be removed.\r\n\r\nFor example\r\n\r\n  x = [1 1 ; 3 0 ; 1 1]\r\n  y = [5 3]\r\n\r\nYour output will be a two column vector that interpolates 5 times between [1 1] and [3 0] (including the boundaries in those five points), and then 3 times between [3 0] and [1 1].  The output should be:\r\n\r\n   1   1\r\n   1.5 0.75\r\n   2   0.5\r\n   2.5 0.25\r\n   3   0\r\n   2   0.5\r\n   1   1\r\n\r\nThere are Y(1)=5 points between the first two coordinate pairs of X [(1,1) and (3,0)], and Y(2)=3 points between the second and third coordinate pairs of X [(3,0) and (2 0.5)].  Notice that there is only one output point for (3,0).\r\n\r\nYou can assume that there will be no complex numbers, Inf, or NaN in x, and Y\u003e=3 for all values of Y.  Good luck!","description_html":"\u003cp\u003eGiven an Mx2 vector and a row of M-1 integers, output a two column vector that linearly interpolates Y times between each successive pair of values in X.  Any consecutive duplicate points are to be removed.\u003c/p\u003e\u003cp\u003eFor example\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003ex = [1 1 ; 3 0 ; 1 1]\r\ny = [5 3]\r\n\u003c/pre\u003e\u003cp\u003eYour output will be a two column vector that interpolates 5 times between [1 1] and [3 0] (including the boundaries in those five points), and then 3 times between [3 0] and [1 1].  The output should be:\u003c/p\u003e\u003cpre\u003e   1   1\r\n   1.5 0.75\r\n   2   0.5\r\n   2.5 0.25\r\n   3   0\r\n   2   0.5\r\n   1   1\u003c/pre\u003e\u003cp\u003eThere are Y(1)=5 points between the first two coordinate pairs of X [(1,1) and (3,0)], and Y(2)=3 points between the second and third coordinate pairs of X [(3,0) and (2 0.5)].  Notice that there is only one output point for (3,0).\u003c/p\u003e\u003cp\u003eYou can assume that there will be no complex numbers, Inf, or NaN in x, and Y\u0026gt;=3 for all values of Y.  Good luck!\u003c/p\u003e","function_template":"function out=tracers(x,y)\r\n\r\n% x is an Mx2 vector, and y is a row of M-1 numbers.\r\n% Tracers linearly interpolates y(1) points between x(1,:) and x(2,:),\r\n% followed by y(2) points between x(2,:) and x(3,:), and so on...\r\n% The resultant output is a two column vector with consecutive\r\n% duplicate points are removed.\r\n\r\nout=3\r\n\r\nend","test_suite":"%%\r\nx=[1 1 ; 3 0 ; 1 1];\r\ny=[5 3];\r\ny_correct=[1 1 ; 1.5 0.75 ; 2 0.5 ; 2.5 0.25 ; 3 0 ; 2 0.5 ; 1 1];\r\nassert(all(all(abs(tracers(x,y)-y_correct)\u003c1e-9)))\r\n%%\r\nx=[1 1 ; 2 3 ; 4 2 ; 5 -2];\r\ny=[6 5 3];\r\ny_correct=[1 1 ; 1.2 1.4 ; 1.4 1.8 ; 1.6 2.2 ; 1.8 2.6 ; 2 3 ; 2.5 2.75 ; 3 2.5 ; 3.5 2.25 ; 4 2 ; 4.5 0 ; 5 -2];\r\nassert(all(all(abs(tracers(x,y)-y_correct)\u003c1e-9)))\r\n%%\r\na=ceil(100*rand)+10;\r\nx=[0 0 ; a a];\r\ny=a+1;\r\ny_correct=[0:a ; 0:a]';\r\nassert(all(all(abs(tracers(x,y)-y_correct)\u003c1e-9)))","published":true,"deleted":false,"likes_count":9,"comments_count":3,"created_by":1615,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":630,"test_suite_updated_at":"2018-04-10T19:49:22.000Z","rescore_all_solutions":true,"group_id":12,"created_at":"2012-10-03T16:25:33.000Z","updated_at":"2026-05-20T01:21:54.000Z","published_at":"2012-10-03T16:31:44.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eGiven an Mx2 vector and a row of M-1 integers, output a two column vector that linearly interpolates Y times between each successive pair of values in X. Any consecutive duplicate points are to be removed.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eFor example\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[x = [1 1 ; 3 0 ; 1 1]\\ny = [5 3]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYour output will be a two column vector that interpolates 5 times between [1 1] and [3 0] (including the boundaries in those five points), and then 3 times between [3 0] and [1 1]. The output 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[   1   1\\n   1.5 0.75\\n   2   0.5\\n   2.5 0.25\\n   3   0\\n   2   0.5\\n   1   1]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThere are Y(1)=5 points between the first two coordinate pairs of X [(1,1) and (3,0)], and Y(2)=3 points between the second and third coordinate pairs of X [(3,0) and (2 0.5)]. Notice that there is only one output point for (3,0).\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYou can assume that there will be no complex numbers, Inf, or NaN in x, and Y\u0026gt;=3 for all values of Y. Good luck!\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"}],"problem_search":{"problems":[{"id":970,"title":"Piecewise linear interpolation","description":"Given an Mx2 vector and a row of M-1 integers, output a two column vector that linearly interpolates Y times between each successive pair of values in X.  Any consecutive duplicate points are to be removed.\r\n\r\nFor example\r\n\r\n  x = [1 1 ; 3 0 ; 1 1]\r\n  y = [5 3]\r\n\r\nYour output will be a two column vector that interpolates 5 times between [1 1] and [3 0] (including the boundaries in those five points), and then 3 times between [3 0] and [1 1].  The output should be:\r\n\r\n   1   1\r\n   1.5 0.75\r\n   2   0.5\r\n   2.5 0.25\r\n   3   0\r\n   2   0.5\r\n   1   1\r\n\r\nThere are Y(1)=5 points between the first two coordinate pairs of X [(1,1) and (3,0)], and Y(2)=3 points between the second and third coordinate pairs of X [(3,0) and (2 0.5)].  Notice that there is only one output point for (3,0).\r\n\r\nYou can assume that there will be no complex numbers, Inf, or NaN in x, and Y\u003e=3 for all values of Y.  Good luck!","description_html":"\u003cp\u003eGiven an Mx2 vector and a row of M-1 integers, output a two column vector that linearly interpolates Y times between each successive pair of values in X.  Any consecutive duplicate points are to be removed.\u003c/p\u003e\u003cp\u003eFor example\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003ex = [1 1 ; 3 0 ; 1 1]\r\ny = [5 3]\r\n\u003c/pre\u003e\u003cp\u003eYour output will be a two column vector that interpolates 5 times between [1 1] and [3 0] (including the boundaries in those five points), and then 3 times between [3 0] and [1 1].  The output should be:\u003c/p\u003e\u003cpre\u003e   1   1\r\n   1.5 0.75\r\n   2   0.5\r\n   2.5 0.25\r\n   3   0\r\n   2   0.5\r\n   1   1\u003c/pre\u003e\u003cp\u003eThere are Y(1)=5 points between the first two coordinate pairs of X [(1,1) and (3,0)], and Y(2)=3 points between the second and third coordinate pairs of X [(3,0) and (2 0.5)].  Notice that there is only one output point for (3,0).\u003c/p\u003e\u003cp\u003eYou can assume that there will be no complex numbers, Inf, or NaN in x, and Y\u0026gt;=3 for all values of Y.  Good luck!\u003c/p\u003e","function_template":"function out=tracers(x,y)\r\n\r\n% x is an Mx2 vector, and y is a row of M-1 numbers.\r\n% Tracers linearly interpolates y(1) points between x(1,:) and x(2,:),\r\n% followed by y(2) points between x(2,:) and x(3,:), and so on...\r\n% The resultant output is a two column vector with consecutive\r\n% duplicate points are removed.\r\n\r\nout=3\r\n\r\nend","test_suite":"%%\r\nx=[1 1 ; 3 0 ; 1 1];\r\ny=[5 3];\r\ny_correct=[1 1 ; 1.5 0.75 ; 2 0.5 ; 2.5 0.25 ; 3 0 ; 2 0.5 ; 1 1];\r\nassert(all(all(abs(tracers(x,y)-y_correct)\u003c1e-9)))\r\n%%\r\nx=[1 1 ; 2 3 ; 4 2 ; 5 -2];\r\ny=[6 5 3];\r\ny_correct=[1 1 ; 1.2 1.4 ; 1.4 1.8 ; 1.6 2.2 ; 1.8 2.6 ; 2 3 ; 2.5 2.75 ; 3 2.5 ; 3.5 2.25 ; 4 2 ; 4.5 0 ; 5 -2];\r\nassert(all(all(abs(tracers(x,y)-y_correct)\u003c1e-9)))\r\n%%\r\na=ceil(100*rand)+10;\r\nx=[0 0 ; a a];\r\ny=a+1;\r\ny_correct=[0:a ; 0:a]';\r\nassert(all(all(abs(tracers(x,y)-y_correct)\u003c1e-9)))","published":true,"deleted":false,"likes_count":9,"comments_count":3,"created_by":1615,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":630,"test_suite_updated_at":"2018-04-10T19:49:22.000Z","rescore_all_solutions":true,"group_id":12,"created_at":"2012-10-03T16:25:33.000Z","updated_at":"2026-05-20T01:21:54.000Z","published_at":"2012-10-03T16:31:44.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eGiven an Mx2 vector and a row of M-1 integers, output a two column vector that linearly interpolates Y times between each successive pair of values in X. Any consecutive duplicate points are to be removed.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eFor example\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[x = [1 1 ; 3 0 ; 1 1]\\ny = [5 3]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYour output will be a two column vector that interpolates 5 times between [1 1] and [3 0] (including the boundaries in those five points), and then 3 times between [3 0] and [1 1]. The output 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[   1   1\\n   1.5 0.75\\n   2   0.5\\n   2.5 0.25\\n   3   0\\n   2   0.5\\n   1   1]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThere are Y(1)=5 points between the first two coordinate pairs of X [(1,1) and (3,0)], and Y(2)=3 points between the second and third coordinate pairs of X [(3,0) and (2 0.5)]. Notice that there is only one output point for (3,0).\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYou can assume that there will be no complex numbers, Inf, or NaN in x, and Y\u0026gt;=3 for all values of Y. Good luck!\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\"}]}"}],"errors":[],"facets":[[{"value":"Indexing I","count":1,"selected":false}],[{"value":"medium","count":1,"selected":false}]],"term":"tag:\"piecewise\"","page":1,"per_page":50,"sort":"map(difficulty_value,0,0,999) asc"}}