필터 지우기
필터 지우기

For loop get an error of "size of the right side is 0-by-1"

조회 수: 9 (최근 30일)
Nao
Nao 2022년 11월 12일
댓글: Nao 2022년 11월 12일
Hi.
I have 2 sets of vectors B and C, both 79x1, and want to extract the max point (peak) between the corresponding rows of B and C. Here is the code I have so far:
peak = B
for i = 1:length(B)
peak(i,1) = max(A(B(i,1):C(1,1)));
end
But this gets an error of "Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 0-by-1".
I have another data set and the above code worked perfectly fine for it. Moreover, when it's taken out of the for loop and I specify a number for "i" for example:
peak(1,1) = max(A(B(1,1):C(1,1)))
This runs without a problem.
Can someone please help me identify the issue? Thank you in advance!

채택된 답변

KSSV
KSSV 2022년 11월 12일
peak = B
for i = 1:length(B)
c = A(B(i,1):C(1,1)) ;
if ~isempty(c)
peak(i,1) = max(c);
end
end
  댓글 수: 3
KSSV
KSSV 2022년 11월 12일
Sometimes the right side is empty. So you cannot assign a empty matrix as an element to in an initialised array. Anyways what you are trying is not a good coding practise.
Nao
Nao 2022년 11월 12일
Ok I understand now. I've still got a lot to learn, thank you for your help!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by