how do i generate a 1X4 matrix with until the sum of the matrix =100

8 ビュー (過去 30 日間)
Austin
Austin 2013 年 11 月 5 日
回答済み: Image Analyst 2013 年 11 月 5 日
how do i generate a 1X4 random matrix until the sum of the matrix equals to 100
with the following condition: (each time the matrix is generated, the last element of the newly generated 1X4 matrix being the first element of last generated 1X4 matrix?) (advise using persistent variables)
  2 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 11 月 5 日
What kind of random numbers, integers? real? its maximum value?
Austin
Austin 2013 年 11 月 5 日
編集済み: Austin 2013 年 11 月 5 日
sorry, its real. no maximum value given, each time the sum of the 4 elements does not add up to 100,it regenerates 4 real numbers again..
a=100*rand(1,4) if sum(a)~=100
.....

サインインしてコメントする。

回答 (4 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 11 月 5 日
The probability to fit your conditions is very small, you can run your code all the year, it's not obvious to get the result.

Image Analyst
Image Analyst 2013 年 11 月 5 日
Try this:
clc;
maxLoops = 1000
loopCounter = 1;
while loopCounter <= maxLoops
theArray = randi(97, 1, 4);
value = sum(theArray);
if value == 100
fprintf('The array = [%d, %d, %d, %d], found after %d tries.\n',...
theArray(1), theArray(2), theArray(3), theArray(4), loopCounter);
break;
end
loopCounter = loopCounter + 1;
end
if loopCounter >= maxLoops % The failsafe kicked in
% Not found.
fprintf('Sum of 100 not found after %d tries.\n', maxLoops);
end
  3 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 11 月 5 日
In his comment, he asks for real numbers
Image Analyst
Image Analyst 2013 年 11 月 5 日
According to the FAQ http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F we know that cannot happen with real numbers, so I used integers.

サインインしてコメントする。


Simon
Simon 2013 年 11 月 5 日
Hi!
Another way may be to create 4 random numbers, take what is missing upto 100 and add to the numbers:
% random numbers
a = 100*rand(1,4);
% missing part
miss = 100 - sum(a);
% choose one possibility
% add to numbers, possibility 1 (linear)
a = a + miss/4;
% add to numbers, possibility 2 (proportional)
a = a + miss*(a/sum(a))
% add to numbers, possibility 3 (???)

Image Analyst
Image Analyst 2013 年 11 月 5 日
Here's a way where you can specify how close you want to get to 100 using floating point numbers. Like Azzi and I said, there's virtually no chance you'll ever hit 100 exactly out to the millionth decimal place. My code below will find numbers that get you to within 0.01, which you can change to get closer if you want.
clc;
maxLoops = 100000
loopCounter = 1;
while loopCounter <= maxLoops
theArray = 97*rand(1, 4);
theSum = sum(theArray);
if abs(theSum - 100) < 0.01
fprintf('The sum = %.5f.\nThe array = [%g, %g, %g, %g], found after %d tries.\n',...
theSum, theArray(1), theArray(2), theArray(3), theArray(4), loopCounter);
break;
end
loopCounter = loopCounter + 1;
end
if loopCounter >= maxLoops % The failsafe kicked in
% Not found.
fprintf('Sum of 100 not found after %d tries.\n', maxLoops);
end

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by