Expression or statement is incomplete or incorrect

5 ビュー (過去 30 日間)
Mark Dela Cruz
Mark Dela Cruz 2017 年 3 月 15 日
編集済み: ES 2017 年 3 月 15 日
I'm getting the above error when I try and run this piece of code. Any help would be appreciated
LVec = (1:20);
for LVec
if LVec == 2 or LVec == 4 or LVec == 6 or LVec = 8 or Lvec = 10 or LVec = 12 or LVec = 14 or LVec = 16 or LVec = 18 or Lvec = 20
LVec = Lvec .* -1
else LVec = LVec
end
end

採用された回答

ES
ES 2017 年 3 月 15 日
編集済み: ES 2017 年 3 月 15 日
you cant use or. Instead use
And your for loop is also incomplete.
May be you wanted to do this..
LVecArray = (1:20);
for loop=1:length(LVecArray)
LVec = LVecArray(loop);
if LVec == 2 || LVec == 4 || LVec == 6 || LVec == 8 || LVec == 10 || LVec == 12 || LVec == 14 || LVec == 16 || LVec == 18 || LVec == 20
LVec = LVec .* -1;
else
LVec = LVec;
end
LVecArray(loop) = LVec;
end
Another solution is
LVecArray = (1:20);
mulArray = repmat([1,-1],1,10)
LVecArray = LVecArray.*mulArray

その他の回答 (1 件)

KSSV
KSSV 2017 年 3 月 15 日
I am not sure what you are trying to do...but this is the code which works:
for LVec = 1:20
if LVec == 2 || LVec == 4 || LVec == 6 || LVec == 8 || LVec == 10 ||..................
LVec == 12 || LVec == 14 || LVec == 16 || LVec == 18 || LVec == 20
LVec = LVec .* -1 ;
else
LVec = LVec
end
end

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by