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

 채택된 답변

Bruno Luong
Bruno Luong 2020년 9월 15일
편집: Bruno Luong 2020년 9월 15일

0 개 추천

Avoid using LENGTH
Replace with
for i = 1:size(x,1)
...
end

댓글 수: 1

Rostislav Stanek
Rostislav Stanek 2020년 9월 15일
Hello Bruno,
this works :-). Thank you very much!
Best regards,
Rostislav

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

BOB MATHEW SYJI
BOB MATHEW SYJI 2020년 9월 15일

0 개 추천

Consider replacing length(x) with numel(x)

댓글 수: 1

Rostislav Stanek
Rostislav Stanek 2020년 9월 15일
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).

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

릴리스

R2020a

태그

질문:

2020년 9월 15일

댓글:

2020년 9월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by