How do I declare a multiline array?
47 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Agnieszka Konstantynowicz
el 4 de Jun. de 2018
Comentada: Walter Roberson
el 30 de Dic. de 2025 a las 22:39
Hello, I have a following problem:
I have to declare an array that is very long and splits into multiple lines. How can I divide it, so that Matlab knows it's one array? Are there any separators I can use at the end of the line?
if true
y = [180 183 188 191 195 199 201 195 207 210 212 216 220 221 224
221 225 229 234 237 239 242 242 244 245 244 246 245 246 247 249
251 252 257 260 263 268 271 272 275 277 279 283 286 291 296 302
306 311 316]
end
2 comentarios
Lakshit
el 30 de Dic. de 2025 a las 18:12
just put " ; " or entre after which you want to make new row
Walter Roberson
el 30 de Dic. de 2025 a las 22:39
Using
[a b c
d e f]
is completely equivalent to using
[a b c;
d e f]
or
[a b c; d e f]
or
[a b c; ...
d e f]
However, considering that the last of the four lines of the original question has only 3 numbers whereas the previous 3 lines have 15 numbers each, we deduce that the actual question is about creating a single row vector but splitting the row vector over several lines. Using ";" would not work for that.
y = [180 183 188 191 195 199 201 195 207 210 212 216 220 221 224;
221 225 229 234 237 239 242 242 244 245 244 246 245 246 247 249;
251 252 257 260 263 268 271 272 275 277 279 283 286 291 296 302;
306 311 316]
would be invalid, an attempt to mix a row of 3 with rows of 15.
The solution is the one proposed by @Stephen23 to put ... at the ends of lines. An alternative way to phrase his contribution would be
y = [180 183 188 191 195 199 201 195 207 210 212 216 220 221 224 ...
221 225 229 234 237 239 242 242 244 245 244 246 245 246 247 249 ...
251 252 257 260 263 268 271 272 275 277 279 283 286 291 296 302 ...
306 311 316]
Note the space after the last number before the ... operator. Several releases ago, MATLAB changed the way that the ... operator was handled; before
[a...
b]
was treated as
[ab]
but now it is treated as
[a b]
To ensure compatiability, it is best to add in the space manually,
Respuesta aceptada
Stephen23
el 4 de Jun. de 2018
Editada: Stephen23
el 4 de Jun. de 2018
y = [180,183,188,191,195,199,201,195,207,210,212,216,220,221,224,...
221,225,229,234,237,239,242,242,244,245,244,246,245,246,247,249,...
251,252,257,260,263,268,271,272,275,277,279,283,286,291,296,302,...
306,311,316]
See "ellipsis" on this page: https://www.mathworks.com/help/matlab/matlab_prog/matlab-operators-and-special-characters.html
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre NaNs 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!