How can I fix this error Index in position 2 exceeds array bounds (must not exceed 1).
23 views (last 30 days)
Show older comments
hala siddiq
on 26 Jun 2021
Commented: Image Analyst
on 26 Jun 2021
leny = length(y);
n = floor(max(time)); % n is the number of different color segments in the plot (1-64)
palette = jet(n); % Create the color palette from the jet color map
% Plot the phase plot
figure
subplot(10,1,1:9)
for t = 1:n
hold on
% Plot part of the phase plot
title('Phase Plot with Colored Time Representation')
pplot1 = plot( y(floor(leny/n)*(t-1)+1 : floor(leny/n)*t,1), ...
y(floor(leny/n)*(t-1)+1 : floor(leny/n)*t,2), ...
'LineWidth',3,'Color', palette(t,:) );
xlabel('x_{1}');
ylabel('x_{2}');
end
hold off
grid on
% Plot the time scale
subplot(10,1,10)
hold on
for t = 1:n
x = [ t-1 t t t-1];
y = [ 0 0 1 1 ];
patch(x,y,palette(t,:))
xlabel('time');
ylabel('Color');
end
hold off
end
I got this message :
Index in position 2 exceeds array bounds (must not exceed 1).
Error in pptime (line 29)
y(floor(leny/n)*(t-1)+1 : floor(leny/n)*t,2), ...
0 Comments
Accepted Answer
Star Strider
on 26 Jun 2021
Since ‘y’ is a (10001x1) column vector, there is no second column as referred to:
y(floor(leny/n)*(t-1)+1 : floor(leny/n)*t,2), ...
↑ ← HERE
Since I have no idea what you want to do, you will need to solve that.
.
2 Comments
Star Strider
on 26 Jun 2021
As always, my pleasure!
The only option that comes quickly to mind (since I am not certain what you are doing) is:
dydt = gradient(y) ./ gradient(time(:));
figure
plot(y, dydt)
grid
Plotting the derivative of the vector against the original vector is a relatively straightforward way of creating a phase plot. (Another way is to plot a delayed version of the original vector against the original vector.)
It would be best to force ‘time’ to also be a column vector, either by transposing it, or using the (:) subscript convention, as I do here. Otherwise, with ‘automatic implicit expansion’ (introduced in R2016b), you could end up inadvertently creating some relatively large matrices that could be confusing to deal with.
.
More Answers (1)
Image Analyst
on 26 Jun 2021
Edited: Image Analyst
on 26 Jun 2021
y is a 1-d vector. There is no second dimension. So you can't tell it to take the second column of y when there is no second column. When an array is either a row vector or a column vector, it treats the first index like a row index, and the second index as a column index. So you have a vector, or range, floor(leny/n)*(t-1)+1 : floor(leny/n)*t for the first index and it will use that as an index (row) into the 1-D y variable. So that part is fine. However you follow that with 2, so now it's expecting that y is a 2-D array, which it's not, and it's wanting to take some elements from the second column. But you declared y as a 1-D variable not a 2-D variable so there is no second dimension (no second column). Not sure what you were thinking but maybe just get rid of the 2 and have:
index1 =floor(leny/n)*(t-1)+1
index2 = floor(leny/n)*t
y(index1 : index2)
that will just extract that range of indexes from the row vector y. Or
pplot1 = plot(y(floor(leny/n)*(t-1)+1 : floor(leny/n)*t), '-', ...
'LineWidth',3, 'Color', palette(t,:) );
3 Comments
Image Analyst
on 26 Jun 2021
I didn't run the code (I don't have plotMD and some of the other functions it calls) but you can change the view point that you're looking at a 3-D plot with with the view() function. Experiment around with that and see if you can get what you want.
See Also
Categories
Find more on Data Distribution Plots 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!