필터 지우기
필터 지우기

unusual subscriptied assignment mismatch

조회 수: 2 (최근 30일)
Sebastian Priebe
Sebastian Priebe 2018년 6월 11일
편집: Jan 2018년 6월 11일
I narrowed the occurence of the error down to some change in the variable b that happens in the 9th iteration of the loop. For some reason the zeros at the end are no longer kept and the assignment fails.
I figured out, that this change happens exactly when b becomes greater than 512, so I guess it has something to do with matlabs handling of doubles? Further, using the uncommented variable makes the code work just fine.
Please help me solve this problem.
var1 = 0.1;
vec = 1:1000;
% length_var = 100.0000;
length_var = 1/(var1 * 0.1);
for ii=1:20
a = round(ii*(0.25*length_var));
b = a - 1 + 3*length_var;
disp(b-a)
c(ii,:) = vec(a:b);
end

채택된 답변

Jan
Jan 2018년 6월 11일
편집: Jan 2018년 6월 11일
You get a warning in each iteration:
Warning: Integer operands are required for colon operator when used as index
var1 = 0.1;
vec = 1:1000;
length_var = 1/(var1 * 0.1);
for ii=1:20
a = round(ii*(0.25*length_var));
b = a - 1 + 3*length_var;
fprintf('%.16g\n', b - a); % 298.9999999999999 in the first 8 iterations
c(ii,:) = vec(a:b);
end
In the 9th iteration b-a becomes 299. Then vec(a:b) has 300 elements, but c(ii,:) has 299 only.
Do not use floating point numbers as indices. You have applied a round for the index a already. Do this for b also. Or even better: Let length_var have an integer value:
fprintf('%.16g', 1/(var1 * 0.1))
99.99999999999999
This is an expected effect when working with floating point numbers stored with limited precision. To get more explanations search in this forum or in the net for "IEEE754".

추가 답변 (1개)

Matt J
Matt J 2018년 6월 11일
var1 = 0.1;
vec = 1:1000;
length_var = round( 1/(var1 * 0.1) );
c=nan(20,3*length_var);
for ii=1:20
a = round(ii*(0.25*length_var));
b = a - 1 + 3*length_var;
disp(b-a)
c(ii,:) = vec(a:b);
end

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by