how to plot index (rownumbers) in a larger dataset as range?
조회 수: 4 (최근 30일)
이전 댓글 표시
Hi,
I'm trying to select certain datavalues with a statement from a larger dataset, resulting in a index array. I need this index array for my plot, however I can't get the index array of equal length with the larger dataset. My code is as follows:
A3 = find(data(:,2)>3.3 & data(:,3)>235 & data(:,3)<275); %index array and statement
figure();
plot(time,data(:,2)); %plot of normal dataset
hold on;
plot(time(A3),data(A3,2),'r.'); %plot that doesn't work (see error code below)
hold off
Resulting in the following error:
Index exceeds the number of array elements (52560).
Error in test_statement (line 88)
plot(time_2018(A3),ws1_2018(A3),'r.');
For your information the data values are as follows:
data 157824x9 double
time 52560x1 double
A3 19967x1 double
댓글 수: 2
Matt J
2023년 1월 20일
편집: Matt J
2023년 1월 20일
Assuming the two variable names that you've shown -- time and time_2018 -- refer to the same variable, they are not the same length as data. So, it is not clear how you would expect the plot would work even if you had run the unindexed version,
plot(time_2018,ws1_2018,'r.');
채택된 답변
dpb
2023년 1월 20일
Presuming you don't try do do the logical addressing until the data array is defined so you don't have the initial error shown, then
ix=data(:,2)>3.3 & iswithin(data(:,3),235,275);
figure();
plot(time,data(:,2));
hold on;
plot(time(ix),data(ix,2),'r.');
hold off
should work fine; note you don't need find here, just use the logical array.
iswithin is my "syntax sugar" utility to reduce the clutter of compound test expressions -- it's quite handy in making things like the above more legible and particularly helpful in expressions.
function flg=iswithin(x,lo,hi)
% returns T for values within range of input
% SYNTAX:
% [log] = iswithin(x,lo,hi)
% returns T for x between lo and hi values, inclusive
flg= (x>=lo) & (x<=hi);
end
>>
One has to presume there's a disconnect owing to an error of some sort such as that shown in the posting that the indexing array being used is out of synch with the actual data array at the time it is being used.
Without the data and complete code snippet that reproduces the error we can't debug that type of an issue remotely...
댓글 수: 3
dpb
2023년 1월 20일
Yes, although my utility function goes back to nearly ver 3.1...long before isbetween came into being.
추가 답변 (1개)
Walter Roberson
2023년 1월 20일
Your data variable has 157824 rows but your time variable only has 52560 rows . You cannot associate each row with a particular time because you have more rows than times.
If you wanted to extract only the first 52560 rows then you could do that.
num_to_use = min(size(data,1), size(time,1));
A3 = find(data(1:num_to_use,2)>3.3 & data(num_to_use,3)>235 & data(1:num_to_use,3)<275);
plot(time(1:num_to_use), data(1:num_to_use,2));
hold on
plot(time(A3), data(A3,2),'r.');
hold off
Or you could use a new facility:
num_to_use = min(size(data,1), size(time,1));
A3 = find(data(1:num_to_use,2)>3.3 & data(num_to_use,3)>235 & data(1:num_to_use,3)<275);
plot(time(1:num_to_use), data(1:num_to_use,2), '.', 'MarkerIndices', A3, 'MarkerFaceColor', 'r');
My system is busy at the moment so I cannot double check whether you would need MarkerEdgeColor instead of MarkerFaceColor for the '.' marker -- some markers use only one of the two properties, other markers use both properties.
댓글 수: 2
Walter Roberson
2023년 1월 22일
num_to_use = min(size(data,1), size(time,1));
A3 = find(data(1:num_to_use,2)>3.3 & data(1:num_to_use,3)>235 & data(1:num_to_use,3)<275);
plot(time(1:num_to_use), data(1:num_to_use,2));
hold on
plot(time(A3), data(A3,2),'r.');
hold off
or
num_to_use = min(size(data,1), size(time,1));
A3 = find(data(1:num_to_use,2)>3.3 & data(1:num_to_use,3)>235 & data(1:num_to_use,3)<275);
plot(time(1:num_to_use), data(1:num_to_use,2), '.', 'MarkerIndices', A3, 'MarkerFaceColor', 'r');
참고 항목
카테고리
Help Center 및 File Exchange에서 Plot Customization에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!