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

4 次查看(过去 30 天)
R1= x1; G1= x2; B1= x3; R2= x4; G2= x5; B2= x6; R3= x7; G3= x8; B3= x9; And so on Please help!
  3 个评论
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.

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by