I want to get total sum of two arrays using for loop. I have made the one but it didn't give expected result. Also i want to multiply these array to get final result. And want to arrange sum and product of two array in a table. Appreciate any help on how to that make using for loop.

2 件のコメント

the cyclist
the cyclist 2021 年 8 月 14 日
Please paste the code here, rather than an image of the code.
Image Analyst
Image Analyst 2021 年 8 月 14 日
Why do you have a nested for loop for 1-D vectors? All you need is one for loop. Post the code, not an image, after you read this:

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

回答 (2 件)

Awais Saeed
Awais Saeed 2021 年 8 月 14 日

1 投票

I highly recommend that you go through the link that @Image Analyst shared. Moreover, for adding elements of two arrays (that's what I understood) you can do the following
x = input('Enter your first array: ');
y = input('Enter your second array: ');
sum_array = zeros(size(x)); % pre-allocation
for col = 1:1:size(x,2)
sum_array(col) = x(col) + y(col);
end
disp(['sum = ',num2str(sum(sum_array))])
For multiplication, use
x.*y % element-wise multiplication

2 件のコメント

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021 年 8 月 14 日
@Awais Saeed. Note that the length of x and y are different and thus, your proposed loop does not work.
Awais Saeed
Awais Saeed 2021 年 8 月 14 日
@Sulaymon Eshkabilov yes you are right. From the picture I assumed that x and y have same length and I wrote the loop for that paticular case.

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

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021 年 8 月 14 日
編集済み: Sulaymon Eshkabilov 2021 年 8 月 15 日

0 投票

In your code, there is an err inside a loop, and here is the correction;
...
% E.g. X and Y can be a row or column vector of any length
...
sum_X=0;
sum_Y=0;
for ii=1:length(X)
sum_X = sum_X+X(ii);
end
for jj=1:length(Y)
sum_Y = sum_Y+Y(jj);
end
sum_XY = sum_X+sum_Y;
fprintf('Total sum: SUM_XY = %f', sum_XY)
% Most correct way is:
sum_X = sum(X);
sum_Y = sum(Y);
sum_XY=sum_X+sum_Y;
% or even more compact one:
sum_XY = sum(X)+sum(Y);
fprintf('Total sum: SUM_XY = %f', sum_XY)

5 件のコメント

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021 年 8 月 14 日
One more point, it is recommended not to use MATLAB's builtin fcn names a variable name, e.g.: sum, min, max, plot, etc.
Yadu Bhusal
Yadu Bhusal 2021 年 8 月 14 日
Thanks a lot. But didn't work well. For example X = [1 2];Y = [3 4]; X+Y should be 10. But answer is different.
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021 年 8 月 14 日
sum_XY = sum_X+sum_Y; has to be outside of the loop and sum_X and sum_Y have to be separated.
...
sum_X=0;
sum_Y=0;
for ii=1:length(X)
sum_X = sum_X+X(ii);
end
for jj=1:length(Y)
sum_Y = sum_Y+Y(jj);
end
sum_XY = sum_X+sum_Y;
fprintf('Total sum: SUM_XY = %f', sum_XY)
Total sum: SUM_XY = 10.000000
% Most correct way is:
sum_X = sum(X);
sum_Y = sum(Y);
sum_XY=sum_X+sum_Y;
% or even more compact one:
sum_XY = sum(X)+sum(Y);
fprintf('Total sum: SUM_XY = %f', sum_XY)
Total sum: SUM_XY = 10.000000
Yadu Bhusal
Yadu Bhusal 2021 年 8 月 14 日
Thank you. It worked well
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021 年 8 月 14 日
Most welcome! So accepted the proposed solutions, right?!

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

カテゴリ

ヘルプ センター および File ExchangeIntroduction to Installation and Licensing についてさらに検索

質問済み:

2021 年 8 月 14 日

編集済み:

2021 年 8 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by