Hi all! Is there a way to initialize row vectors like this:

14 ビュー (過去 30 日間)
Huma Jabeen
Huma Jabeen 2018 年 2 月 20 日
コメント済み: Stephen23 2018 年 2 月 20 日
R1= x1; G1= x2; B1= x3; R2= x4; G2= x5; B2= x6; R3= x7; G3= x8; B3= x9; And so on Please help!
  3 件のコメント
Huma Jabeen
Huma Jabeen 2018 年 2 月 20 日
No, it's a single row vector.
Stephen23
Stephen23 2018 年 2 月 20 日
Why split them up? MATLAB works best when you keep data together as much as possible.
Just use indexing: indexing is simple, neat, easy to debug, and very efficient. The best solution is to use one array right from the very start, so that you can write simpler, more efficient code, and not to make copies of your data and make it harder to work with.
You should read this:
It is possible to do what you asked for, but the MATLAB documentation states: "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended. The preferred method is to store related data in a single array"

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

採用された回答

Shounak Shastri
Shounak Shastri 2018 年 2 月 20 日
編集済み: Shounak Shastri 2018 年 2 月 20 日
"No, it's a single row vector."
Based on this I will assume that R,G and B are also row vectors.
Say,
x=1:30
You can divide elements in x as
R=x(1:3:end-2);
G=x(2:3:end-1);
B=x(3:3:end);
This will give you R, G, B as row vectors with elements from x.

その他の回答 (1 件)

Birdman
Birdman 2018 年 2 月 20 日
There are several ways to do it. For instance, you can symbolically initialize a row vector as
>>x=sym('R',[1 9]);
x =
[ R1, R2, ..., R9]
or you may use str2sym function if you have R2017b version.
>>x=["R1","R2","R3",...,"R9"];
str2sym(x)
x =
[ R1, R2, ..., R9]
or by using a for loop, you can do it as follows:
x=sym(zeros(1,9));
for i=1:numel(x)
x(i)=sym(sprintf('R%i',i));
end
Hope this helps.
  1 件のコメント
Shounak Shastri
Shounak Shastri 2018 年 2 月 20 日
編集済み: Shounak Shastri 2018 年 2 月 20 日
for loop wins my vote.

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

カテゴリ

Help Center および File ExchangeSymbolic Math Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by