Could anyone help me how to solve the issue.

1 回表示 (過去 30 日間)
jaah navi
jaah navi 2019 年 4 月 16 日
編集済み: Stephen23 2019 年 4 月 16 日
how to add all the values generated using for loop
for example
A=[1 2],[3 4],[7 5 6]
for i=1:length(A)
value% i am using formula to calculate the value
end
In this code first it run for [1 2] generates a value-(a)
then it run for [3] generates a value-(b)
then it run for [4 5 6] and generates a value-(c)
Now i want to add (a)+(b)+(c) after the end statement so i tried with the follwing command sum(value,:) but unable to get the result.
Could anyone please help me on this.

採用された回答

Stephen23
Stephen23 2019 年 4 月 16 日
編集済み: Stephen23 2019 年 4 月 16 日
Using very basic MATLAB indexing:
A = {[1,2],[3,4],[7,5,6]};
N = numel(A);
Z = nan(1,N);
for k = 1:N
Z(k) ... your code using A{k}
end
sum(Z)
@ jaah navi: you have been using MATLAB since 2017, loops and indexing are very basic programming concepts that you should definitely learn yourself, they are explained in the introductory tutorials:
  3 件のコメント
jaah navi
jaah navi 2019 年 4 月 16 日
with respect to your sample code
A = {[1,2],[3,4],[7,5,6]};
N = numel(A);
Z = nan(1,N);
for k = 1:N
Z(k) ... your code using A{k}
end
sum(Z)
The code is for A={[1,2],[3,4],[7,5,6]};
in the same manner i have different numbers for A
for example
when
A = {[1,2],[3,4],[7,5,6]};
the output can be obtained by sum(Z)
when A={[4 5 ],[1 3 4]
i can get the output by sum(Z)
now i need to add both sum(Z)
could you please help me on this.
Stephen23
Stephen23 2019 年 4 月 16 日
編集済み: Stephen23 2019 年 4 月 16 日
jaah navi : you wrote in your original question that you have some code that "generates a value-(a)" and "generates a value-(b)" and "generates a value-(c)". You did not show us what that code is, or explain anything about how it works. However you made it clear that it returns a scalar "value". All you need to do is use indexing to put that scalar value into the numeric array Z, exactly as my answer shows you. The cell array A is irrelevant to this: my code uses numel to adjust to whatever size A happens to be.
If your code does not actually return a scalar value then you need to revise your question and explain the your data much better.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOperators and Elementary Operations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by