{"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":44779,"title":"Don't be mean.  Be nice!","description":"For this problem, you will be given a range of single digits R, and a separate number K.  You job is to calculate the mean of all K digit numbers that contain only distinct digits from the range R.\r\n\r\nFor example, if R=1:4 and K=2, you should calculate the mean of 12, 13, 14, 21, 23, 24, 31, 32, 34, 41, 42, and 43, as these are all of the two digit numbers that contain two distinct numbers from the range 1:4.  The numbers 11, 22, 33 and 44 are not included, as they contain multiple copies of the same digit.\r\n\r\nIf 0 is included in R, it should not be a leading digit for any of the numbers, so an R of 0:2 and K=3 would include:\r\n\r\n* 120\r\n* 210\r\n* 201\r\n* 102\r\n\r\nbut not 012 or 021 for the purposes of this calculation.\r\n\r\nYou can assume that R will always have at least K digits, and there will be no repeating digits in R.","description_html":"\u003cp\u003eFor this problem, you will be given a range of single digits R, and a separate number K.  You job is to calculate the mean of all K digit numbers that contain only distinct digits from the range R.\u003c/p\u003e\u003cp\u003eFor example, if R=1:4 and K=2, you should calculate the mean of 12, 13, 14, 21, 23, 24, 31, 32, 34, 41, 42, and 43, as these are all of the two digit numbers that contain two distinct numbers from the range 1:4.  The numbers 11, 22, 33 and 44 are not included, as they contain multiple copies of the same digit.\u003c/p\u003e\u003cp\u003eIf 0 is included in R, it should not be a leading digit for any of the numbers, so an R of 0:2 and K=3 would include:\u003c/p\u003e\u003cul\u003e\u003cli\u003e120\u003c/li\u003e\u003cli\u003e210\u003c/li\u003e\u003cli\u003e201\u003c/li\u003e\u003cli\u003e102\u003c/li\u003e\u003c/ul\u003e\u003cp\u003ebut not 012 or 021 for the purposes of this calculation.\u003c/p\u003e\u003cp\u003eYou can assume that R will always have at least K digits, and there will be no repeating digits in R.\u003c/p\u003e","function_template":"function y = dont_be_mean(R,k)\r\n  y = k.^R;\r\nend","test_suite":"%%\r\nR=1:4;k=2;y_correct = 27.5;\r\na=dont_be_mean(R,k)\r\nassert(abs(a-y_correct)\u003c1e-10)\r\n%%\r\nR=0:8;k=3;y_correct = 493.3125;\r\na=dont_be_mean(R,k)\r\nassert(abs(a-y_correct)\u003c1e-10)\r\n%%\r\nR=[1 2 4 6 8];k=4;y_correct = 4666.2;\r\na=dont_be_mean(R,k)\r\nassert(abs(a-y_correct)\u003c1e-10)\r\n%%\r\nR=[2 8 6 7 4 5];k=1;y_correct = 5.33333333333333;\r\na=dont_be_mean(R,k)\r\nassert(abs(a-y_correct)\u003c1e-10)\r\n%%\r\nR=0:9;\r\ny=0;\r\nfor k=1:8\r\n    y=y+dont_be_mean(R,k);\r\nend\r\ny_correct=61042519.44444444;\r\nassert(abs(y-y_correct)\u003c1e-3)\r\n","published":true,"deleted":false,"likes_count":2,"comments_count":1,"created_by":1615,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":38,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":68,"created_at":"2018-11-07T18:32:08.000Z","updated_at":"2026-04-09T15:34:44.000Z","published_at":"2018-11-07T18:32:08.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eFor this problem, you will be given a range of single digits R, and a separate number K. You job is to calculate the mean of all K digit numbers that contain only distinct digits from the range R.\u003c/w:t\u003e\u003c/w:r\u003e\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, if R=1:4 and K=2, you should calculate the mean of 12, 13, 14, 21, 23, 24, 31, 32, 34, 41, 42, and 43, as these are all of the two digit numbers that contain two distinct numbers from the range 1:4. The numbers 11, 22, 33 and 44 are not included, as they contain multiple copies of the same digit.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eIf 0 is included in R, it should not be a leading digit for any of the numbers, so an R of 0:2 and K=3 would include:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e120\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e210\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e201\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e102\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ebut not 012 or 021 for the purposes of this calculation.\u003c/w:t\u003e\u003c/w:r\u003e\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 R will always have at least K digits, and there will be no repeating digits in R.\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":42503,"title":"Generating random matrix with given probability mass function","description":"Inspired by \u003chttp://www.mathworks.com/matlabcentral/cody/problems/2356-simulating-the-selection-of-a-state-with-given-probabilities Problem 2356. Simulating the selection of a state with given probabilities\u003e, let's consider a similar yet more useful problem. Write a function\r\n\r\n                             x = rndsampling(m,n,prob)\r\n\r\nto generate an m-by-n matrix x, whose entries are drawn independently from integer symbols 1:numel(prob) according to the given probability mass function prob. Specifically, symbol k occurs with probability prob(k), k = 1, 2, ..., numel(prob), where all(prob\u003e0) == 1 and sum(prob) == 1.","description_html":"\u003cp\u003eInspired by \u003ca href = \"http://www.mathworks.com/matlabcentral/cody/problems/2356-simulating-the-selection-of-a-state-with-given-probabilities\"\u003eProblem 2356. Simulating the selection of a state with given probabilities\u003c/a\u003e, let's consider a similar yet more useful problem. Write a function\u003c/p\u003e\u003cpre\u003e                             x = rndsampling(m,n,prob)\u003c/pre\u003e\u003cp\u003eto generate an m-by-n matrix x, whose entries are drawn independently from integer symbols 1:numel(prob) according to the given probability mass function prob. Specifically, symbol k occurs with probability prob(k), k = 1, 2, ..., numel(prob), where all(prob\u0026gt;0) == 1 and sum(prob) == 1.\u003c/p\u003e","function_template":"function x = rndsampling(m,n,prob);\r\n  x = rand(m,n)\r\nend","test_suite":"%%\r\nrnd = sort(rand(randi([10,20]),1));\r\nprob = vertcat(rnd(1,:),diff(rnd,1,1),1-rnd(end,:));\r\nsz = [1 1e5;1e5 1;1e3 1e2;randi([100 200], 100, 2)];\r\nsz = sz(randi(size(sz,1)),:);\r\nx = rndsampling(sz(1),sz(2),prob);\r\nprob_est = histcounts(x,1:numel(prob)+1,'Normalization','probability').';\r\nerr = mean(abs(prob_est - prob))\r\nassert(err \u003c 0.005 \u0026\u0026 isequal(size(x),sz) \u0026\u0026 all(~isnan(x(:))));\r\n","published":true,"deleted":false,"likes_count":2,"comments_count":1,"created_by":12569,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":107,"test_suite_updated_at":"2015-08-13T18:44:59.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2015-08-11T19:26:49.000Z","updated_at":"2026-02-02T05:18:21.000Z","published_at":"2015-08-11T19:26: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\",\"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\u003eInspired by\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://www.mathworks.com/matlabcentral/cody/problems/2356-simulating-the-selection-of-a-state-with-given-probabilities\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eProblem 2356. Simulating the selection of a state with given probabilities\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e, let's consider a similar yet more useful problem. Write a function\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 = rndsampling(m,n,prob)]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eto generate an m-by-n matrix x, whose entries are drawn independently from integer symbols 1:numel(prob) according to the given probability mass function prob. Specifically, symbol k occurs with probability prob(k), k = 1, 2, ..., numel(prob), where all(prob\u0026gt;0) == 1 and sum(prob) == 1.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":44676,"title":"Mean = Standard Deviation","description":"Create a series with following properties;\r\n\r\n# All of the members should be positive integer\r\n# Mean of the series should be integer\r\n# Standard deviation of the series should be integer (use a normalization factor of N instead of N-1)\r\n# Mean should be equal to standard deviation\r\n\r\nFor example if input is 6, you can return the following series;\r\n\r\n  out = [12 44 2 24 2 6]\r\n\r\n  mean(out) = 15;\r\n  std(out,1) = 15;\r\n  \r\nAnother example; if input is 4, a possible solution;\r\n\r\n  out = [24 2 2 48];\r\n  mean(out) = 19;\r\n  std(out,1) = 19;\r\n\r\n  ","description_html":"\u003cp\u003eCreate a series with following properties;\u003c/p\u003e\u003col\u003e\u003cli\u003eAll of the members should be positive integer\u003c/li\u003e\u003cli\u003eMean of the series should be integer\u003c/li\u003e\u003cli\u003eStandard deviation of the series should be integer (use a normalization factor of N instead of N-1)\u003c/li\u003e\u003cli\u003eMean should be equal to standard deviation\u003c/li\u003e\u003c/ol\u003e\u003cp\u003eFor example if input is 6, you can return the following series;\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eout = [12 44 2 24 2 6]\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003emean(out) = 15;\r\nstd(out,1) = 15;\r\n\u003c/pre\u003e\u003cp\u003eAnother example; if input is 4, a possible solution;\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eout = [24 2 2 48];\r\nmean(out) = 19;\r\nstd(out,1) = 19;\r\n\u003c/pre\u003e","function_template":"function y = meanEqualsStd(x)\r\n  y = x;\r\nend","test_suite":"%%\r\nfor idx = 5:50\r\n    x = idx;\r\n    Series = meanEqualsStd(x);\r\n    mSeries = mean(Series);\r\n    sSeries = std(Series,1);\r\n    assert(length(Series)==x);\r\n    assert(round(mSeries)==mSeries);\r\n    assert(round(sSeries)==sSeries);\r\n    assert(all(round(Series) == Series));\r\n    assert(all(round(Series) \u003e= 1));\r\n    assert(mSeries == sSeries);\r\nend\r\n\r\n%%\r\nfiletext = fileread('meanEqualsStd.m');\r\nassert(isempty(strfind(filetext, 'echo')))\r\nassert(isempty(strfind(filetext, 'assert')))","published":true,"deleted":false,"likes_count":5,"comments_count":11,"created_by":8703,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":9,"test_suite_updated_at":"2018-06-14T07:03:27.000Z","rescore_all_solutions":true,"group_id":1,"created_at":"2018-06-07T01:30:24.000Z","updated_at":"2025-11-14T13:50:04.000Z","published_at":"2018-06-07T01:30:24.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\u003eCreate a series with following properties;\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"2\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eAll of the members should be positive integer\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"2\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eMean of the series should be integer\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"2\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eStandard deviation of the series should be integer (use a normalization factor of N instead of N-1)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"2\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eMean should be equal to standard deviation\u003c/w:t\u003e\u003c/w:r\u003e\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 if input is 6, you can return the following series;\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[out = [12 44 2 24 2 6]\\n\\nmean(out) = 15;\\nstd(out,1) = 15;]]\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\u003eAnother example; if input is 4, a possible solution;\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[out = [24 2 2 48];\\nmean(out) = 19;\\nstd(out,1) = 19;]]\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":3009,"title":"Test Driven Solution - Probability Problem 3","description":"*Problem:* Without any Cody cheats, write code that passes the test suite.\r\n\r\n*Hint:* The test suite gets samples from the probability distribution represented by your code.  A cumulative distribution function is then built from the samples.  This is the empirical distribution, which is compared against the theoretical distribution you must infer from the test suite.\r\n\r\n*See also:* \u003chttp://en.wikipedia.org/wiki/Cumulative_distribution_function Cumulative Distribution Function\u003e\r\n\r\n*Problems in Series:* \u003chttp://www.mathworks.com/matlabcentral/cody/problems/2995-test-driven-solution-probability-problem-1 Probability Problem 1\u003e, \u003chttp://www.mathworks.com/matlabcentral/cody/problems/3006-test-driven-solution-probability-problem-2 Probability Problem 2\u003e","description_html":"\u003cp\u003e\u003cb\u003eProblem:\u003c/b\u003e Without any Cody cheats, write code that passes the test suite.\u003c/p\u003e\u003cp\u003e\u003cb\u003eHint:\u003c/b\u003e The test suite gets samples from the probability distribution represented by your code.  A cumulative distribution function is then built from the samples.  This is the empirical distribution, which is compared against the theoretical distribution you must infer from the test suite.\u003c/p\u003e\u003cp\u003e\u003cb\u003eSee also:\u003c/b\u003e \u003ca href = \"http://en.wikipedia.org/wiki/Cumulative_distribution_function\"\u003eCumulative Distribution Function\u003c/a\u003e\u003c/p\u003e\u003cp\u003e\u003cb\u003eProblems in Series:\u003c/b\u003e \u003ca href = \"http://www.mathworks.com/matlabcentral/cody/problems/2995-test-driven-solution-probability-problem-1\"\u003eProbability Problem 1\u003c/a\u003e, \u003ca href = \"http://www.mathworks.com/matlabcentral/cody/problems/3006-test-driven-solution-probability-problem-2\"\u003eProbability Problem 2\u003c/a\u003e\u003c/p\u003e","function_template":"function vec = fcn(len)\r\n  vec = nan(len, 1);\r\nend","test_suite":"%%\r\n% test for correct size\r\nfor iter = 1:10\r\n  vectorLength = randi([1 100]);\r\n  result       = fcn(vectorLength);\r\n  assert(isequal([vectorLength, 1], size(result)));\r\nend\r\n\r\n%%\r\n% get large sample\r\nvectorLength = 10000;\r\nresult       = fcn(vectorLength);\r\n\r\n% built empirical cumulative distribution function\r\nxEmpirical = 0:10;                     % x-axis\r\ncounts     = accumarray(result+1, 1);\r\ndensity    = counts ./ sum(counts);\r\nyEmpirical = cumsum(density);          % y-axis\r\n\r\n% build theoretical cumulative distribution function\r\nxTheoretical = xEmpirical; % x-axis\r\nfor k = xTheoretical\r\n  yTheoretical(k+1, 1) = betainc(exp(-1), 10-k, k+1); % y-axis\r\nend\r\n\r\n% compute statistics on diff between empirical and theoretical\r\nerrorList = abs(yEmpirical - yTheoretical);\r\nerrorMax  = max(errorList);\r\nerrorSum  = sum(errorList);\r\nerrorStd  = std(errorList);\r\n\r\n% if fcn is correct, this should pass at least 99.9% of the time\r\nassert(errorMax \u003c .018);\r\nassert(errorSum \u003e .0045);\r\nassert(errorStd \u003e .0004);","published":true,"deleted":false,"likes_count":2,"comments_count":0,"created_by":692,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":13,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2015-02-12T17:08:10.000Z","updated_at":"2026-04-09T15:45:38.000Z","published_at":"2015-02-12T17:08:43.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:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eProblem:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Without any Cody cheats, write code that passes the test suite.\u003c/w:t\u003e\u003c/w:r\u003e\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\u003eHint:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e The test suite gets samples from the probability distribution represented by your code. A cumulative distribution function is then built from the samples. This is the empirical distribution, which is compared against the theoretical distribution you must infer from the test suite.\u003c/w:t\u003e\u003c/w:r\u003e\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\u003eSee also:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://en.wikipedia.org/wiki/Cumulative_distribution_function\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eCumulative Distribution Function\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=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eProblems in Series:\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/2995-test-driven-solution-probability-problem-1\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eProbability Problem 1\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e,\u003c/w:t\u003e\u003c/w:r\u003e\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/3006-test-driven-solution-probability-problem-2\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eProbability Problem 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":331,"title":"Compute Area from Fixed Sum Cumulative Probability","description":"In Matlab the code\r\n v = rand(1,3);\r\n v = v/sum(v);\r\nis sometimes suggested as a convenient means of generating three random variables, whose ranges are restricted to [0,1], which have a fixed sum of one. However, this procedure has the property that the area-wise density distribution of the three values, considered as cartesian coordinates in 3D space, is widely variable throughout the planar region of possible locations of v. For any given density value in the range of this density distribution, let A be the corresponding area of the subregion of all points whose density is less than or equal to this given value, and let P be the corresponding probability that v would lie in this subregion. The task is to write a function 'fixedsumarea' which receives P as an input and gives A as an output:\r\n A = fixedsumarea(P);\r\nYou should assume that initially 'rand(1,3)' perfectly generates three independent random variables each uniformly distributed on [0,1], but subsequently each is modified by being divided by their mutual sum.","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: 311.3px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 407px 155.65px; transform-origin: 407px 155.65px; 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: 58px 8px; transform-origin: 58px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eIn Matlab the code\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgb(247, 247, 247); block-size: 40.8667px; 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 20.4333px; transform-origin: 404px 20.4333px; 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: 60px 8.5px; tab-size: 4; transform-origin: 60px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e v = rand(1,3);\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: 56px 8.5px; tab-size: 4; transform-origin: 56px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e v = v/sum(v);\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 147px; 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 73.5px; text-align: left; transform-origin: 384px 73.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: 371.5px 8px; transform-origin: 371.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eis sometimes suggested as a convenient means of generating three random variables, whose ranges are restricted to [0,1], which have a fixed sum of one. However, this procedure has the property that the area-wise density distribution of the three values, considered as cartesian coordinates in 3D space, is widely variable throughout the planar region of possible locations of v. For any given density value in the range of this density distribution, let A be the corresponding area of the subregion of all points whose density is less than or equal to this given value, and let P be the corresponding probability that v would lie in this subregion. The task is to write a function 'fixedsumarea' which receives P as an input and gives A as an output:\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgb(247, 247, 247); block-size: 20.4333px; 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 10.2167px; transform-origin: 404px 10.2167px; margin-left: 3px; margin-top: 10px; margin-bottom: 10px; margin-right: 3px; \"\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-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; 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: 84px 8.5px; tab-size: 4; transform-origin: 84px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e A = fixedsumarea(P);\u003c/span\u003e\u003c/span\u003e\u003c/div\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: 10px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 10px; perspective-origin: 384px 21px; text-align: left; transform-origin: 384px 21px; 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: 362.5px 8px; transform-origin: 362.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eYou should assume that initially 'rand(1,3)' perfectly generates three independent random variables each uniformly distributed on [0,1], but subsequently each is modified by being divided by their mutual sum.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function A = fixedsumarea(P)\r\n  P = 1/2;\r\n  A = 0;\r\nend","test_suite":"%%\r\nfiletext = fileread('fixedsumarea.m');\r\nillegal = contains(filetext, 'assignin') || contains(filetext, 'regexp') || ...\r\n          contains(filetext, 'elseif') || contains(filetext, 'switch ');\r\nassert(~illegal)\r\n\r\n%%\r\nP = pi/4;\r\nA_correct = 0.7984235067141288;\r\nassert(abs(fixedsumarea(P)-A_correct)\u003c100*eps)\r\n%%\r\nP = 1/sqrt(11);\r\nA_correct = 0.4964013344766580;\r\nassert(abs(fixedsumarea(P)-A_correct)\u003c100*eps)\r\n%%\r\nP = exp(-3);\r\nA_correct = 0.1494793760894695;\r\nassert(abs(fixedsumarea(P)-A_correct)\u003c100*eps)\r\n%%\r\nP = (1/27)^(1/5);\r\nA_correct = 0.6605992894366502;\r\nassert(abs(fixedsumarea(P)-A_correct)\u003c100*eps)\r\n%%\r\nP = sin(sqrt(2));\r\nA_correct = 0.8634048022602919;\r\nassert(abs(fixedsumarea(P)-A_correct)\u003c100*eps)\r\n%%\r\nP = 68/137;\r\nA_correct = 0.6471420329484348;\r\nassert(abs(fixedsumarea(P)-A_correct)\u003c100*eps)\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":7,"created_by":28,"edited_by":223089,"edited_at":"2023-02-21T09:48:12.000Z","deleted_by":null,"deleted_at":null,"solvers_count":5,"test_suite_updated_at":"2023-02-21T09:48:12.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2012-02-17T05:58:59.000Z","updated_at":"2025-10-20T16:39:20.000Z","published_at":"2012-02-17T18:47:40.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\u003eIn Matlab the code\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[ v = rand(1,3);\\n v = v/sum(v);]]\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\u003eis sometimes suggested as a convenient means of generating three random variables, whose ranges are restricted to [0,1], which have a fixed sum of one. However, this procedure has the property that the area-wise density distribution of the three values, considered as cartesian coordinates in 3D space, is widely variable throughout the planar region of possible locations of v. For any given density value in the range of this density distribution, let A be the corresponding area of the subregion of all points whose density is less than or equal to this given value, and let P be the corresponding probability that v would lie in this subregion. The task is to write a function 'fixedsumarea' which receives P as an input and gives A as an output:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ A = fixedsumarea(P);]]\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 should assume that initially 'rand(1,3)' perfectly generates three independent random variables each uniformly distributed on [0,1], but subsequently each is modified by being divided by their mutual sum.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\",\"relationship\":null}],\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"target\":\"/matlab/document.xml\",\"relationshipId\":\"rId1\"}]}"}],"problem_search":{"errors":[],"problems":[{"id":44779,"title":"Don't be mean.  Be nice!","description":"For this problem, you will be given a range of single digits R, and a separate number K.  You job is to calculate the mean of all K digit numbers that contain only distinct digits from the range R.\r\n\r\nFor example, if R=1:4 and K=2, you should calculate the mean of 12, 13, 14, 21, 23, 24, 31, 32, 34, 41, 42, and 43, as these are all of the two digit numbers that contain two distinct numbers from the range 1:4.  The numbers 11, 22, 33 and 44 are not included, as they contain multiple copies of the same digit.\r\n\r\nIf 0 is included in R, it should not be a leading digit for any of the numbers, so an R of 0:2 and K=3 would include:\r\n\r\n* 120\r\n* 210\r\n* 201\r\n* 102\r\n\r\nbut not 012 or 021 for the purposes of this calculation.\r\n\r\nYou can assume that R will always have at least K digits, and there will be no repeating digits in R.","description_html":"\u003cp\u003eFor this problem, you will be given a range of single digits R, and a separate number K.  You job is to calculate the mean of all K digit numbers that contain only distinct digits from the range R.\u003c/p\u003e\u003cp\u003eFor example, if R=1:4 and K=2, you should calculate the mean of 12, 13, 14, 21, 23, 24, 31, 32, 34, 41, 42, and 43, as these are all of the two digit numbers that contain two distinct numbers from the range 1:4.  The numbers 11, 22, 33 and 44 are not included, as they contain multiple copies of the same digit.\u003c/p\u003e\u003cp\u003eIf 0 is included in R, it should not be a leading digit for any of the numbers, so an R of 0:2 and K=3 would include:\u003c/p\u003e\u003cul\u003e\u003cli\u003e120\u003c/li\u003e\u003cli\u003e210\u003c/li\u003e\u003cli\u003e201\u003c/li\u003e\u003cli\u003e102\u003c/li\u003e\u003c/ul\u003e\u003cp\u003ebut not 012 or 021 for the purposes of this calculation.\u003c/p\u003e\u003cp\u003eYou can assume that R will always have at least K digits, and there will be no repeating digits in R.\u003c/p\u003e","function_template":"function y = dont_be_mean(R,k)\r\n  y = k.^R;\r\nend","test_suite":"%%\r\nR=1:4;k=2;y_correct = 27.5;\r\na=dont_be_mean(R,k)\r\nassert(abs(a-y_correct)\u003c1e-10)\r\n%%\r\nR=0:8;k=3;y_correct = 493.3125;\r\na=dont_be_mean(R,k)\r\nassert(abs(a-y_correct)\u003c1e-10)\r\n%%\r\nR=[1 2 4 6 8];k=4;y_correct = 4666.2;\r\na=dont_be_mean(R,k)\r\nassert(abs(a-y_correct)\u003c1e-10)\r\n%%\r\nR=[2 8 6 7 4 5];k=1;y_correct = 5.33333333333333;\r\na=dont_be_mean(R,k)\r\nassert(abs(a-y_correct)\u003c1e-10)\r\n%%\r\nR=0:9;\r\ny=0;\r\nfor k=1:8\r\n    y=y+dont_be_mean(R,k);\r\nend\r\ny_correct=61042519.44444444;\r\nassert(abs(y-y_correct)\u003c1e-3)\r\n","published":true,"deleted":false,"likes_count":2,"comments_count":1,"created_by":1615,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":38,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":68,"created_at":"2018-11-07T18:32:08.000Z","updated_at":"2026-04-09T15:34:44.000Z","published_at":"2018-11-07T18:32:08.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eFor this problem, you will be given a range of single digits R, and a separate number K. You job is to calculate the mean of all K digit numbers that contain only distinct digits from the range R.\u003c/w:t\u003e\u003c/w:r\u003e\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, if R=1:4 and K=2, you should calculate the mean of 12, 13, 14, 21, 23, 24, 31, 32, 34, 41, 42, and 43, as these are all of the two digit numbers that contain two distinct numbers from the range 1:4. The numbers 11, 22, 33 and 44 are not included, as they contain multiple copies of the same digit.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eIf 0 is included in R, it should not be a leading digit for any of the numbers, so an R of 0:2 and K=3 would include:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e120\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e210\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e201\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e102\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ebut not 012 or 021 for the purposes of this calculation.\u003c/w:t\u003e\u003c/w:r\u003e\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 R will always have at least K digits, and there will be no repeating digits in R.\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":42503,"title":"Generating random matrix with given probability mass function","description":"Inspired by \u003chttp://www.mathworks.com/matlabcentral/cody/problems/2356-simulating-the-selection-of-a-state-with-given-probabilities Problem 2356. Simulating the selection of a state with given probabilities\u003e, let's consider a similar yet more useful problem. Write a function\r\n\r\n                             x = rndsampling(m,n,prob)\r\n\r\nto generate an m-by-n matrix x, whose entries are drawn independently from integer symbols 1:numel(prob) according to the given probability mass function prob. Specifically, symbol k occurs with probability prob(k), k = 1, 2, ..., numel(prob), where all(prob\u003e0) == 1 and sum(prob) == 1.","description_html":"\u003cp\u003eInspired by \u003ca href = \"http://www.mathworks.com/matlabcentral/cody/problems/2356-simulating-the-selection-of-a-state-with-given-probabilities\"\u003eProblem 2356. Simulating the selection of a state with given probabilities\u003c/a\u003e, let's consider a similar yet more useful problem. Write a function\u003c/p\u003e\u003cpre\u003e                             x = rndsampling(m,n,prob)\u003c/pre\u003e\u003cp\u003eto generate an m-by-n matrix x, whose entries are drawn independently from integer symbols 1:numel(prob) according to the given probability mass function prob. Specifically, symbol k occurs with probability prob(k), k = 1, 2, ..., numel(prob), where all(prob\u0026gt;0) == 1 and sum(prob) == 1.\u003c/p\u003e","function_template":"function x = rndsampling(m,n,prob);\r\n  x = rand(m,n)\r\nend","test_suite":"%%\r\nrnd = sort(rand(randi([10,20]),1));\r\nprob = vertcat(rnd(1,:),diff(rnd,1,1),1-rnd(end,:));\r\nsz = [1 1e5;1e5 1;1e3 1e2;randi([100 200], 100, 2)];\r\nsz = sz(randi(size(sz,1)),:);\r\nx = rndsampling(sz(1),sz(2),prob);\r\nprob_est = histcounts(x,1:numel(prob)+1,'Normalization','probability').';\r\nerr = mean(abs(prob_est - prob))\r\nassert(err \u003c 0.005 \u0026\u0026 isequal(size(x),sz) \u0026\u0026 all(~isnan(x(:))));\r\n","published":true,"deleted":false,"likes_count":2,"comments_count":1,"created_by":12569,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":107,"test_suite_updated_at":"2015-08-13T18:44:59.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2015-08-11T19:26:49.000Z","updated_at":"2026-02-02T05:18:21.000Z","published_at":"2015-08-11T19:26: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\",\"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\u003eInspired by\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://www.mathworks.com/matlabcentral/cody/problems/2356-simulating-the-selection-of-a-state-with-given-probabilities\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eProblem 2356. Simulating the selection of a state with given probabilities\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e, let's consider a similar yet more useful problem. Write a function\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 = rndsampling(m,n,prob)]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eto generate an m-by-n matrix x, whose entries are drawn independently from integer symbols 1:numel(prob) according to the given probability mass function prob. Specifically, symbol k occurs with probability prob(k), k = 1, 2, ..., numel(prob), where all(prob\u0026gt;0) == 1 and sum(prob) == 1.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":44676,"title":"Mean = Standard Deviation","description":"Create a series with following properties;\r\n\r\n# All of the members should be positive integer\r\n# Mean of the series should be integer\r\n# Standard deviation of the series should be integer (use a normalization factor of N instead of N-1)\r\n# Mean should be equal to standard deviation\r\n\r\nFor example if input is 6, you can return the following series;\r\n\r\n  out = [12 44 2 24 2 6]\r\n\r\n  mean(out) = 15;\r\n  std(out,1) = 15;\r\n  \r\nAnother example; if input is 4, a possible solution;\r\n\r\n  out = [24 2 2 48];\r\n  mean(out) = 19;\r\n  std(out,1) = 19;\r\n\r\n  ","description_html":"\u003cp\u003eCreate a series with following properties;\u003c/p\u003e\u003col\u003e\u003cli\u003eAll of the members should be positive integer\u003c/li\u003e\u003cli\u003eMean of the series should be integer\u003c/li\u003e\u003cli\u003eStandard deviation of the series should be integer (use a normalization factor of N instead of N-1)\u003c/li\u003e\u003cli\u003eMean should be equal to standard deviation\u003c/li\u003e\u003c/ol\u003e\u003cp\u003eFor example if input is 6, you can return the following series;\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eout = [12 44 2 24 2 6]\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003emean(out) = 15;\r\nstd(out,1) = 15;\r\n\u003c/pre\u003e\u003cp\u003eAnother example; if input is 4, a possible solution;\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eout = [24 2 2 48];\r\nmean(out) = 19;\r\nstd(out,1) = 19;\r\n\u003c/pre\u003e","function_template":"function y = meanEqualsStd(x)\r\n  y = x;\r\nend","test_suite":"%%\r\nfor idx = 5:50\r\n    x = idx;\r\n    Series = meanEqualsStd(x);\r\n    mSeries = mean(Series);\r\n    sSeries = std(Series,1);\r\n    assert(length(Series)==x);\r\n    assert(round(mSeries)==mSeries);\r\n    assert(round(sSeries)==sSeries);\r\n    assert(all(round(Series) == Series));\r\n    assert(all(round(Series) \u003e= 1));\r\n    assert(mSeries == sSeries);\r\nend\r\n\r\n%%\r\nfiletext = fileread('meanEqualsStd.m');\r\nassert(isempty(strfind(filetext, 'echo')))\r\nassert(isempty(strfind(filetext, 'assert')))","published":true,"deleted":false,"likes_count":5,"comments_count":11,"created_by":8703,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":9,"test_suite_updated_at":"2018-06-14T07:03:27.000Z","rescore_all_solutions":true,"group_id":1,"created_at":"2018-06-07T01:30:24.000Z","updated_at":"2025-11-14T13:50:04.000Z","published_at":"2018-06-07T01:30:24.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\u003eCreate a series with following properties;\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"2\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eAll of the members should be positive integer\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"2\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eMean of the series should be integer\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"2\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eStandard deviation of the series should be integer (use a normalization factor of N instead of N-1)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"2\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eMean should be equal to standard deviation\u003c/w:t\u003e\u003c/w:r\u003e\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 if input is 6, you can return the following series;\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[out = [12 44 2 24 2 6]\\n\\nmean(out) = 15;\\nstd(out,1) = 15;]]\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\u003eAnother example; if input is 4, a possible solution;\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[out = [24 2 2 48];\\nmean(out) = 19;\\nstd(out,1) = 19;]]\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":3009,"title":"Test Driven Solution - Probability Problem 3","description":"*Problem:* Without any Cody cheats, write code that passes the test suite.\r\n\r\n*Hint:* The test suite gets samples from the probability distribution represented by your code.  A cumulative distribution function is then built from the samples.  This is the empirical distribution, which is compared against the theoretical distribution you must infer from the test suite.\r\n\r\n*See also:* \u003chttp://en.wikipedia.org/wiki/Cumulative_distribution_function Cumulative Distribution Function\u003e\r\n\r\n*Problems in Series:* \u003chttp://www.mathworks.com/matlabcentral/cody/problems/2995-test-driven-solution-probability-problem-1 Probability Problem 1\u003e, \u003chttp://www.mathworks.com/matlabcentral/cody/problems/3006-test-driven-solution-probability-problem-2 Probability Problem 2\u003e","description_html":"\u003cp\u003e\u003cb\u003eProblem:\u003c/b\u003e Without any Cody cheats, write code that passes the test suite.\u003c/p\u003e\u003cp\u003e\u003cb\u003eHint:\u003c/b\u003e The test suite gets samples from the probability distribution represented by your code.  A cumulative distribution function is then built from the samples.  This is the empirical distribution, which is compared against the theoretical distribution you must infer from the test suite.\u003c/p\u003e\u003cp\u003e\u003cb\u003eSee also:\u003c/b\u003e \u003ca href = \"http://en.wikipedia.org/wiki/Cumulative_distribution_function\"\u003eCumulative Distribution Function\u003c/a\u003e\u003c/p\u003e\u003cp\u003e\u003cb\u003eProblems in Series:\u003c/b\u003e \u003ca href = \"http://www.mathworks.com/matlabcentral/cody/problems/2995-test-driven-solution-probability-problem-1\"\u003eProbability Problem 1\u003c/a\u003e, \u003ca href = \"http://www.mathworks.com/matlabcentral/cody/problems/3006-test-driven-solution-probability-problem-2\"\u003eProbability Problem 2\u003c/a\u003e\u003c/p\u003e","function_template":"function vec = fcn(len)\r\n  vec = nan(len, 1);\r\nend","test_suite":"%%\r\n% test for correct size\r\nfor iter = 1:10\r\n  vectorLength = randi([1 100]);\r\n  result       = fcn(vectorLength);\r\n  assert(isequal([vectorLength, 1], size(result)));\r\nend\r\n\r\n%%\r\n% get large sample\r\nvectorLength = 10000;\r\nresult       = fcn(vectorLength);\r\n\r\n% built empirical cumulative distribution function\r\nxEmpirical = 0:10;                     % x-axis\r\ncounts     = accumarray(result+1, 1);\r\ndensity    = counts ./ sum(counts);\r\nyEmpirical = cumsum(density);          % y-axis\r\n\r\n% build theoretical cumulative distribution function\r\nxTheoretical = xEmpirical; % x-axis\r\nfor k = xTheoretical\r\n  yTheoretical(k+1, 1) = betainc(exp(-1), 10-k, k+1); % y-axis\r\nend\r\n\r\n% compute statistics on diff between empirical and theoretical\r\nerrorList = abs(yEmpirical - yTheoretical);\r\nerrorMax  = max(errorList);\r\nerrorSum  = sum(errorList);\r\nerrorStd  = std(errorList);\r\n\r\n% if fcn is correct, this should pass at least 99.9% of the time\r\nassert(errorMax \u003c .018);\r\nassert(errorSum \u003e .0045);\r\nassert(errorStd \u003e .0004);","published":true,"deleted":false,"likes_count":2,"comments_count":0,"created_by":692,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":13,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2015-02-12T17:08:10.000Z","updated_at":"2026-04-09T15:45:38.000Z","published_at":"2015-02-12T17:08:43.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:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eProblem:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Without any Cody cheats, write code that passes the test suite.\u003c/w:t\u003e\u003c/w:r\u003e\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\u003eHint:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e The test suite gets samples from the probability distribution represented by your code. A cumulative distribution function is then built from the samples. This is the empirical distribution, which is compared against the theoretical distribution you must infer from the test suite.\u003c/w:t\u003e\u003c/w:r\u003e\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\u003eSee also:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://en.wikipedia.org/wiki/Cumulative_distribution_function\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eCumulative Distribution Function\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=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eProblems in Series:\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/2995-test-driven-solution-probability-problem-1\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eProbability Problem 1\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e,\u003c/w:t\u003e\u003c/w:r\u003e\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/3006-test-driven-solution-probability-problem-2\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eProbability Problem 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":331,"title":"Compute Area from Fixed Sum Cumulative Probability","description":"In Matlab the code\r\n v = rand(1,3);\r\n v = v/sum(v);\r\nis sometimes suggested as a convenient means of generating three random variables, whose ranges are restricted to [0,1], which have a fixed sum of one. However, this procedure has the property that the area-wise density distribution of the three values, considered as cartesian coordinates in 3D space, is widely variable throughout the planar region of possible locations of v. For any given density value in the range of this density distribution, let A be the corresponding area of the subregion of all points whose density is less than or equal to this given value, and let P be the corresponding probability that v would lie in this subregion. The task is to write a function 'fixedsumarea' which receives P as an input and gives A as an output:\r\n A = fixedsumarea(P);\r\nYou should assume that initially 'rand(1,3)' perfectly generates three independent random variables each uniformly distributed on [0,1], but subsequently each is modified by being divided by their mutual sum.","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: 311.3px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 407px 155.65px; transform-origin: 407px 155.65px; 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: 58px 8px; transform-origin: 58px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eIn Matlab the code\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgb(247, 247, 247); block-size: 40.8667px; 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 20.4333px; transform-origin: 404px 20.4333px; 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: 60px 8.5px; tab-size: 4; transform-origin: 60px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e v = rand(1,3);\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: 56px 8.5px; tab-size: 4; transform-origin: 56px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e v = v/sum(v);\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 147px; 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 73.5px; text-align: left; transform-origin: 384px 73.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: 371.5px 8px; transform-origin: 371.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eis sometimes suggested as a convenient means of generating three random variables, whose ranges are restricted to [0,1], which have a fixed sum of one. However, this procedure has the property that the area-wise density distribution of the three values, considered as cartesian coordinates in 3D space, is widely variable throughout the planar region of possible locations of v. For any given density value in the range of this density distribution, let A be the corresponding area of the subregion of all points whose density is less than or equal to this given value, and let P be the corresponding probability that v would lie in this subregion. The task is to write a function 'fixedsumarea' which receives P as an input and gives A as an output:\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgb(247, 247, 247); block-size: 20.4333px; 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 10.2167px; transform-origin: 404px 10.2167px; margin-left: 3px; margin-top: 10px; margin-bottom: 10px; margin-right: 3px; \"\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-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; 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: 84px 8.5px; tab-size: 4; transform-origin: 84px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e A = fixedsumarea(P);\u003c/span\u003e\u003c/span\u003e\u003c/div\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: 10px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 10px; perspective-origin: 384px 21px; text-align: left; transform-origin: 384px 21px; 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: 362.5px 8px; transform-origin: 362.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eYou should assume that initially 'rand(1,3)' perfectly generates three independent random variables each uniformly distributed on [0,1], but subsequently each is modified by being divided by their mutual sum.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function A = fixedsumarea(P)\r\n  P = 1/2;\r\n  A = 0;\r\nend","test_suite":"%%\r\nfiletext = fileread('fixedsumarea.m');\r\nillegal = contains(filetext, 'assignin') || contains(filetext, 'regexp') || ...\r\n          contains(filetext, 'elseif') || contains(filetext, 'switch ');\r\nassert(~illegal)\r\n\r\n%%\r\nP = pi/4;\r\nA_correct = 0.7984235067141288;\r\nassert(abs(fixedsumarea(P)-A_correct)\u003c100*eps)\r\n%%\r\nP = 1/sqrt(11);\r\nA_correct = 0.4964013344766580;\r\nassert(abs(fixedsumarea(P)-A_correct)\u003c100*eps)\r\n%%\r\nP = exp(-3);\r\nA_correct = 0.1494793760894695;\r\nassert(abs(fixedsumarea(P)-A_correct)\u003c100*eps)\r\n%%\r\nP = (1/27)^(1/5);\r\nA_correct = 0.6605992894366502;\r\nassert(abs(fixedsumarea(P)-A_correct)\u003c100*eps)\r\n%%\r\nP = sin(sqrt(2));\r\nA_correct = 0.8634048022602919;\r\nassert(abs(fixedsumarea(P)-A_correct)\u003c100*eps)\r\n%%\r\nP = 68/137;\r\nA_correct = 0.6471420329484348;\r\nassert(abs(fixedsumarea(P)-A_correct)\u003c100*eps)\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":7,"created_by":28,"edited_by":223089,"edited_at":"2023-02-21T09:48:12.000Z","deleted_by":null,"deleted_at":null,"solvers_count":5,"test_suite_updated_at":"2023-02-21T09:48:12.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2012-02-17T05:58:59.000Z","updated_at":"2025-10-20T16:39:20.000Z","published_at":"2012-02-17T18:47:40.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\u003eIn Matlab the code\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[ v = rand(1,3);\\n v = v/sum(v);]]\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\u003eis sometimes suggested as a convenient means of generating three random variables, whose ranges are restricted to [0,1], which have a fixed sum of one. However, this procedure has the property that the area-wise density distribution of the three values, considered as cartesian coordinates in 3D space, is widely variable throughout the planar region of possible locations of v. For any given density value in the range of this density distribution, let A be the corresponding area of the subregion of all points whose density is less than or equal to this given value, and let P be the corresponding probability that v would lie in this subregion. The task is to write a function 'fixedsumarea' which receives P as an input and gives A as an output:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ A = fixedsumarea(P);]]\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 should assume that initially 'rand(1,3)' perfectly generates three independent random variables each uniformly distributed on [0,1], but subsequently each is modified by being divided by their mutual sum.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\",\"relationship\":null}],\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"target\":\"/matlab/document.xml\",\"relationshipId\":\"rId1\"}]}"}],"term":"group:\"Probability \u0026 Stats\" difficulty_rating_bin:hard","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:\"Probability \u0026 Stats\" difficulty_rating_bin:hard","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"group":[["group:\"Probability \u0026 Stats\"","","\"","Probability \u0026 Stats","\""]],"difficulty_rating_bin":[["difficulty_rating_bin:hard","","","hard",""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007f64c6c4b878\u003e":["Probability \u0026 Stats"],"#\u003cMathWorks::Search::Field:0x00007f64c6c4b7d8\u003e":["hard"]},"filters":{"#\u003cMathWorks::Search::Field:0x00007f64c6c4af18\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007f64c6c4baf8\u003e":1,"#\u003cMathWorks::Search::Field:0x00007f64c6c4ba58\u003e":50,"#\u003cMathWorks::Search::Field:0x00007f64c6c4b9b8\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007f64c6c4b918\u003e":"group:\"Probability \u0026 Stats\" difficulty_rating_bin:hard"},"user_query":{"#\u003cMathWorks::Search::Field:0x00007f64c6c4b918\u003e":"group:\"Probability \u0026 Stats\" difficulty_rating_bin:hard"},"queried_facets":{"#\u003cMathWorks::Search::Field:0x00007f64c6c4b878\u003e":["Probability \u0026 Stats"],"#\u003cMathWorks::Search::Field:0x00007f64c6c4b7d8\u003e":["hard"]}},"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:\"Probability \u0026 Stats\" difficulty_rating_bin:hard","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"group":[["group:\"Probability \u0026 Stats\"","","\"","Probability \u0026 Stats","\""]],"difficulty_rating_bin":[["difficulty_rating_bin:hard","","","hard",""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007f64c6c4b878\u003e":["Probability \u0026 Stats"],"#\u003cMathWorks::Search::Field:0x00007f64c6c4b7d8\u003e":["hard"]},"filters":{"#\u003cMathWorks::Search::Field:0x00007f64c6c4af18\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007f64c6c4baf8\u003e":1,"#\u003cMathWorks::Search::Field:0x00007f64c6c4ba58\u003e":50,"#\u003cMathWorks::Search::Field:0x00007f64c6c4b9b8\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007f64c6c4b918\u003e":"group:\"Probability \u0026 Stats\" difficulty_rating_bin:hard"},"user_query":{"#\u003cMathWorks::Search::Field:0x00007f64c6c4b918\u003e":"group:\"Probability \u0026 Stats\" difficulty_rating_bin:hard"},"queried_facets":{"#\u003cMathWorks::Search::Field:0x00007f64c6c4b878\u003e":["Probability \u0026 Stats"],"#\u003cMathWorks::Search::Field:0x00007f64c6c4b7d8\u003e":["hard"]}},"options":{"fields":["id","difficulty_rating"]},"join":" "},"results":[{"id":44779,"difficulty_rating":"medium-hard"},{"id":42503,"difficulty_rating":"medium-hard"},{"id":44676,"difficulty_rating":"medium-hard"},{"id":3009,"difficulty_rating":"medium-hard"},{"id":331,"difficulty_rating":"hard"}]}}