フィルターのクリア

Problem with calculating a product of a random vector using a For loop. Help, Urgent!

1 回表示 (過去 30 日間)
Hi guys, I have some coursework, seems quite simple from what I was doing a year ago but I seem to have forgotten everything. This is the question:
Develop a script with name to calculate the product of the elements of a vector.
1) Create a vector variable (Vect) to store a set of 10 random numbers, which are generated by rand function with rand(1, 10).
2) Using a for loop to compute the product of the elements in Vect.
3) Store the product to a variable P and display its value. Note that you can use the built in function prod to check if your program is correct.
I cant seem to figure out the code, I can use prod at the end but I cannot for the life of me seem to get the product from a For loop.
Here is my code so far:
Vect=rand(1,10);
N=length(Vect);
RunSum=1;
for n=1:N
Z=RunSum*Vect(n);
end
P=prod(Vect);
disp(Z)
disp(P)
Any help would be HUGELY appreciated. I have 6 more questions to do after this and it builds from it, thanks in advance! (remember, I can only use prod to check my For loop is correct).
  2 件のコメント
Andy Keane
Andy Keane 2017 年 4 月 23 日
Thanks mate, sory for my late reply. I have been so bogged down. I owe a lot of thanks to you guys here. I passed the module, so thanks for your help!

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

採用された回答

James Tursa
James Tursa 2016 年 11 月 1 日
編集済み: James Tursa 2016 年 11 月 1 日
RunSum = RunSum*Vect(n);
:
disp(RunSum)
Although, I think the instructions meant for your code to use the variable name P for your output (instead of RunSum). And then use something else for the result of the prod function (e.g., prodcheck).
  3 件のコメント
Andy Keane
Andy Keane 2016 年 11 月 1 日
YOU BEAUTY!!!!! How did I miss that? Panic set in I guess! I owe you several pints, seriously. I even read Rudra Prataps whole book and no answer in it!
James Tursa
James Tursa 2016 年 11 月 1 日
The colon : was just there to indicate snipped code in my answer to make it easier to read. I did not intend for you to physically have that colon in your code or for you to physically snip anything out of your code.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by