フィルターのクリア

Help with my for loop please?

1 回表示 (過去 30 日間)
Dave
Dave 2013 年 2 月 28 日
Hi,
I wrote some code below but it's not exactly how I want it; it's more like a demonstration of my ideal outcome. I'm trying to symbolically assign subscripts as the for loop goes on...so every time there's a u_i-1 or u_i-2..it's ideally meant to be u_(i-1) and u_(i-2).
My problem though is that I don't have a way of assigning symbolic subscripts within the for loop and as of right now the only way I can fix this is by manually editing each index of my matrix: Vector
Any help greatly appreciated :) :)
(PS I know a few variables are unassigned in my syms string, I have use for them later ^_^)
clear all
clc
syms P A_0 U L E j u x u_i
N = 4;
h = L/N;
k=N+2;
for i=1:k
vec = [u_i-2:u_i-1]+i;
mat(i,:) = vec;
Vector = fliplr(mat);
end
Vector

採用された回答

Walter Roberson
Walter Roberson 2013 年 2 月 28 日
If u_(i-1) is a symbolic expression then you are not going to be able to use u_(i-2):u_(i-1) because the ":" operator is not defined for symbols.
If the u_ are numeric then there is no need to worry about symbolically assigning subscripts; leave u_ as a numeric vector and access u_(i-2) and u_(i-1)
  3 件のコメント
Walter Roberson
Walter Roberson 2013 年 2 月 28 日
I do not see the point. If the size of your vec can vary between iterations then you cannot store vec into mat(i,:), and if the size of vec does not vary then you do not need to go through the trouble of working with symbolic endpoints.
If there is some good reason to use symbolic endpoints then remember you can create a vector of symbols and index the vector.
If you are using R2011b or later, perhaps you want something like
u_ = syms('u', [1 10]);
to create the vector.
Dave
Dave 2013 年 2 月 28 日
I think that gets me closer to what I want but the subscripts won't be organized properly unless the for loop can categorize the 'Vector' matrix using your suggestion, but like you said before about ":" not defining symbols, I think this is dead in the water..
Also I think you meant to say 'sym' as in
u_ = sym('u', [1 10])
:)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAssumptions についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by