Zeroing the start of my vectors with a for loop

2 ビュー (過去 30 日間)
Daniel
Daniel 2015 年 11 月 12 日
編集済み: Stephen23 2019 年 6 月 19 日
Hello, I have 6 vectors. Gauge1, Gauge2, Gauge3...Gauge6. They do not all start with a 0. So I want to reset each vector with a for loop.
for i=1:6
Gauge(i)=Gauge(i)-Gauge(i)(1);
end
So it should subtract all numbers in the vector with the first number in the vector...
But I get this from matlab ------ ()-indexing must appear last in an index expression. ------
What should I change. Thank you

採用された回答

dpb
dpb 2015 年 11 月 12 日
編集済み: dpb 2015 年 11 月 12 日
"I have 6 vectors. Gauge1, Gauge2, Gauge3..."
Don't name variables this way!!!! See the FAQ for why not: Avoid using the eval function
As you see, you can't address these variables easily as Matlab can't interpret variable names dynamically for ordinary arrays. Instead create a single array gauges of size [N,6]. Then the above operation becomes trivial--
gauges=bsxfun(@minus,gauges,gauges(1,:));
See
doc bsxfun % for details on the singleton expansion to subtract the one row from the rest

その他の回答 (1 件)

Stephen23
Stephen23 2015 年 11 月 13 日
編集済み: Stephen23 2019 年 6 月 19 日

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by