how to plot index (rownumbers) in a larger dataset as range?
9 views (last 30 days)
Show older comments
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
Accepted Answer
dpb
on 20 Jan 2023
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...
More Answers (1)
Walter Roberson
on 20 Jan 2023
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 Comments
Walter Roberson
on 22 Jan 2023
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');
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!