Running through a randomly generated array and doing calculations

2 ビュー (過去 30 日間)
Ian
Ian 2014 年 2 月 4 日
編集済み: Mischa Kim 2014 年 2 月 4 日
Here's the problem statement: "Consider an array x of randomly generated positive and negative integers. Write a script that performs the following iteration: starting with 0 it goes through the entries of x, adds them to the total if they are positive and multiplies them by the total if they are negative."
The array x has been provided. It's a 1x20 matrix named HW1Rand
Here's the code I've got so far:
HW1_6a = 0; %initialization
for ii = HW1Rand(1,1:20)
if ii>0
HW_6a == HW_6a + ii
else
HW_6a == HW_6a * ii
end
end
However I keep getting an error. Please help!
  1 件のコメント
Ian
Ian 2014 年 2 月 4 日
note: there are no zeros in the array HW1Rand!

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

採用された回答

Mischa Kim
Mischa Kim 2014 年 2 月 4 日
編集済み: Mischa Kim 2014 年 2 月 4 日
Ian, what is HW1Rand(1,1:20), in other words, how is the function/matrix defined? Also, for assignments in MATLAB you'd use a simple =. The == is a relational operator.
Finally, you are initializing HW1_6a but use HW_6a in the calculations.
HW_6a = 0; % initialization
for ii = HW1Rand(1,1:20) % assuming HW1Rand is properly defined
if ii>0
HW_6a = HW_6a + ii
else
HW_6a = HW_6a * ii
end
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by