create indexes of x in a for loop

2 ビュー (過去 30 日間)
Nikolas Spiliopoulos
Nikolas Spiliopoulos 2018 年 11 月 14 日
コメント済み: Nikolas Spiliopoulos 2018 年 11 月 15 日
Hi all, I am trying to create and save some x values in an array in a format of A=[x(2)+x(3);x(3)+x(6);x(4)+x(9)];
so I tried something like that
for i=1:3
y=@(x) x(i+1)+x(3*i);
end
however I get for each i the same expression,
y=@(x)x(i+1)+x(3*i)
Is there any way to do that?
thanks
Nikolas

回答 (1 件)

Stephen23
Stephen23 2018 年 11 月 14 日
編集済み: Stephen23 2018 年 11 月 14 日
It is not very clear what you are trying to achieve, but it is possible to store a function handle in a cell array:
C = cell(1,3);
for k=1:3
C{k} = @(x) x(k+1)+x(3*k);
end
Although to be honest it is not clear why you are defining a function handle at all. If x is already defined, why not just get the numeric values directly:
N = nan(1,3);
for k=1:3
N(k) = x(k+1)+x(3*k);
end
  9 件のコメント
Stephen23
Stephen23 2018 年 11 月 15 日
"anyway my problem if there is any way to do symbolic indexing"
What exactly is "symbolic indexing"?
Perhaps a simple function of x is what you are looking for... nothing to do with indexing at all.
Nikolas Spiliopoulos
Nikolas Spiliopoulos 2018 年 11 月 15 日
well my problem is, if I have a symbolic variable x,
is it possible to create somehow something like
A=[ x(1)+x(2) ; X(3)+x(4)] etc.
by using an index i for example?

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

カテゴリ

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