Indices shown in plot are off

For large vectors, the indices shown in a figure are a bit off. For example:
test = [zeros(1,1079998), 1, zeros(1,1457106-1079998), 1, zeros(1,2159999-1457108)];
find(test) % gives correct indices: 1079999 1457108
figure; plot(test)
% The labeling of the 1's in the plot using data tips give idices of 1080000 and 1457110
This is probably a numeric precision issue.

답변 (1개)

Torsten
Torsten 2024년 5월 17일
편집: Torsten 2024년 5월 17일

0 개 추천

My guess is that the x-axis has a resolution of 10.

댓글 수: 7

Davis
Davis 2024년 5월 20일
This is not generally true, for the same length vector:
test = [zeros(1,107998), 1, zeros(1,145716-107998), 1, zeros(1,2159999-145718)];
find(test) % gives 107999 and 145718
figure; plot(test)
% The labeling of the 1's in the plot using data tips give also idices of 107999 and 145718
Torsten
Torsten 2024년 5월 20일
Yes, because your new data vector has 1/10 of the elements of the first. Thus its resolution can be chosen as 1.
Try for a data vector with 10x the number of elements of the first. My guess is that the resolution achieved is 100.
Davis
Davis 2024년 5월 20일
I didn't change the vector size; I just moved the non-zero points.
% first vector
test = [zeros(1,1079998), 1, zeros(1,1457106-1079998), 1, zeros(1,2159999-1457108)];
length(test)
% second vector
test = [zeros(1,107998), 1, zeros(1,145716-107998), 1, zeros(1,2159999-145718)];
length(test)
Furthermore. the resolution is location dependent:
test = [zeros(1,107998), 1, zeros(1,1457106-107998), 1, zeros(1,2159999-1457108)];
find(test) % gives 107999 and 1457108
figure; plot(test); % data tips at 107999 (correct) and 1457110 (incorrect)
Davis
Davis 2024년 5월 21일
Thanks Torsten, I submitted this issue to Mathworks using the link you provided. I'll update when I hear back.
Davis
Davis 2024년 5월 22일
편집: Davis 2024년 5월 22일
I got the following response from Mathworks:
After initial investigation, the description matches our record and indicates this is likely a known bug. The potential reason is that the datatip is using "%g" as output format. I want to let you know that our development team is aware of this issue. We appreciate your effort in identifying and reporting issues to help us provide our users with better experience using MATLAB.
Meanwhile you could manually set the datatip output format with the following code:
test = [zeros(1,1079998), 1, zeros(1,1457106-1079998), 1, zeros(1,2159999-1457108)];
find(test) % gives correct indices: 1079999 1457108
figure;
p = plot(test)
p.DataTipTemplate.DataTipRows(1).Format ='%d'
Torsten
Torsten 2024년 5월 22일
Thank you for coming back and posting TMW's answer here.

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

카테고리

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

제품

릴리스

R2023b

질문:

2024년 5월 17일

댓글:

2024년 5월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by