Hi all! Is there a way to initialize row vectors like this:
14 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Huma Jabeen
el 20 de Feb. de 2018
Comentada: Stephen23
el 20 de Feb. de 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 comentarios
Stephen23
el 20 de Feb. de 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"
Respuesta aceptada
Shounak Shastri
el 20 de Feb. de 2018
Editada: Shounak Shastri
el 20 de Feb. de 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.
0 comentarios
Más respuestas (1)
Birdman
el 20 de Feb. de 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 comentario
Shounak Shastri
el 20 de Feb. de 2018
Editada: Shounak Shastri
el 20 de Feb. de 2018
for loop wins my vote.
Ver también
Categorías
Más información sobre Symbolic Math Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!