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

14 views (last 30 days)
R1= x1; G1= x2; B1= x3; R2= x4; G2= x5; B2= x6; R3= x7; G3= x8; B3= x9; And so on Please help!
  3 Comments
Stephen23
Stephen23 on 20 Feb 2018
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"

Sign in to comment.

Accepted Answer

Shounak Shastri
Shounak Shastri on 20 Feb 2018
Edited: Shounak Shastri on 20 Feb 2018
"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.

More Answers (1)

Birdman
Birdman on 20 Feb 2018
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.

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!