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

14 vues (au cours des 30 derniers jours)
Huma Jabeen
Huma Jabeen le 20 Fév 2018
Commenté : Stephen23 le 20 Fév 2018
R1= x1; G1= x2; B1= x3; R2= x4; G2= x5; B2= x6; R3= x7; G3= x8; B3= x9; And so on Please help!
  3 commentaires
Huma Jabeen
Huma Jabeen le 20 Fév 2018
No, it's a single row vector.
Stephen23
Stephen23 le 20 Fév 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"

Connectez-vous pour commenter.

Réponse acceptée

Shounak Shastri
Shounak Shastri le 20 Fév 2018
Modifié(e) : Shounak Shastri le 20 Fév 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.

Plus de réponses (1)

Birdman
Birdman le 20 Fév 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.
  1 commentaire
Shounak Shastri
Shounak Shastri le 20 Fév 2018
Modifié(e) : Shounak Shastri le 20 Fév 2018
for loop wins my vote.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Symbolic Math Toolbox dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by