Here is simple code where I would like to remove for loop. A=[9;9;9]; c1=[2;3;5] for i=1:length(c1) c=c1(i,1); [B]=Subsum (A,c) i=i+1; end
Function subsum is defined as below
function [B] = Subsum (A,c) B=A-c; end
Desired output First loop B=[ 7 ;7;7] second loop B=[6;6;6] and Third loop B=[4;4;4]

 採用された回答

madhan ravi
madhan ravi 2018 年 11 月 7 日
編集済み: madhan ravi 2018 年 11 月 7 日

1 投票

A=[9;9;9];
c=[2;3;5];
b={(A-c)};
B=cell(1,length(c));
for i = 1:length(B)
B{i}=repmat(b{:}(i),numel(c),1);
end
celldisp(B)
command window
>> COMMUNITY
B{1} =
7
7
7
B{2} =
6
6
6
B{3} =
4
4
4
>>

4 件のコメント

madhan ravi
madhan ravi 2018 年 11 月 7 日
what's your desired output?
Praveen
Praveen 2018 年 11 月 7 日
Thanks for the answer in case I want output as first loop B=[ 7 ;7;7] second loop B=[6;6;6] and Third loop B=[4;4;4]
Then code is A=[9;9;9]; c1=[2; 3; 5]; for i=1:length(c1) c=c1(i,1) [B] = Subsum (A,c) i=i+1; end
How can I write this without loop
Praveen
Praveen 2018 年 11 月 7 日
Your Answer was correct but I changed my question a bit
madhan ravi
madhan ravi 2018 年 11 月 7 日
if it was correct make sure to accept the answer

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

タグ

質問済み:

2018 年 11 月 7 日

編集済み:

2018 年 11 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by