Matrix having one row

3 visualizaciones (últimos 30 días)
Rostislav Stanek
Rostislav Stanek el 15 de Sept. de 2020
Comentada: Rostislav Stanek el 15 de Sept. de 2020
Hello,
I have the following problem: I would like to create a vecor of vectors containing pairs of numbers and then iterate through it. If I have a vector containing more than one pair, everything is OK:
x = [[-2, -1.5]; [-0.5, 0.5]; [2, 2.5]];
for i = 1:length(x)
%operations with x(i)
end
Unfortunately, if the input is a vactor containing only one pair, the program crashes:
x = [[-2, -1.5]];
for i = 1:length(x)
%operations with x(i)
end
I understand, why the program crashes, but I do not know, how to solve it. With other words: How can I design a general program, which works for both possibility without necessity of changing the loop?
Thank you for your help!
Rostislav

Respuesta aceptada

Bruno Luong
Bruno Luong el 15 de Sept. de 2020
Editada: Bruno Luong el 15 de Sept. de 2020
Avoid using LENGTH
Replace with
for i = 1:size(x,1)
...
end
  1 comentario
Rostislav Stanek
Rostislav Stanek el 15 de Sept. de 2020
Hello Bruno,
this works :-). Thank you very much!
Best regards,
Rostislav

Iniciar sesión para comentar.

Más respuestas (1)

BOB MATHEW SYJI
BOB MATHEW SYJI el 15 de Sept. de 2020
Consider replacing length(x) with numel(x)
  1 comentario
Rostislav Stanek
Rostislav Stanek el 15 de Sept. de 2020
Length and numel yield the same.
>> x = [[-2, -1.5]];
>> numel(x)
ans =
2
>> length(x)
ans =
2
I would like to get 1 in order to have a vector of length 2 in x(1) (or x(i) in general).

Iniciar sesión para comentar.

Categorías

Más información sobre Graphics Object Programming en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by