Quarter values of a vector for plotting
1 view (last 30 days)
Show older comments
Hello,
I have a matrix which correspondes to the X and Y coordinates, the matrix is of size (2,m) where m is unknown. On my plot i want to label the plot at 25%, 50% and 75% of the plot (as well as the inintial and final values but i managed with those two). Is there an easy clean way to implement this or do i need to manually find the quarter points of the vector. this is what i have at the moment:
first_q = round(size(f,2)*0.25);
second_q = round(size(f,2)*0.5);
third_q = round(size(f,2)*0.75);
text( f(1,1), f(2,1), sprintf('initial - %.2f', f(2,1)) )
text( f(1,first_q), f(2,first_q), sprintf( '25%% - %.2f', f(2,first_q)), 'HorizontalAlignment', 'center')
text( f(1,second_q), f(2,second_q), sprintf( '50%% - %.2f', f(2,second_q)), 'HorizontalAlignment', 'center')
text( f(1,third_q), f(2,third_q), sprintf( '75%% - %.2f', f(2,third_q)), 'HorizontalAlignment', 'center')
text( f(1,end), f(2,end), sprintf( 'final - %.2f', f(end)), 'HorizontalAlignment', 'right')
Answers (1)
Aniket
on 10 Jun 2025
Yes, you do need to manually find the indices corresponding to 25%, 50%, and 75% of the vector length. There is no built-in shorthand in MATLAB for “label at 25% of a vector's length” — so your use of round(size(f,2)*0.25) is actually standard and perfectly fine.
However, the code can be cleaned up slightly following below steps:
- Avoid repeating size(f,2) by assigning it once.
- Use a loop to reduce code repetition.
- Fix small typo: f(end) in your final text call should be f(2,end) for the y-value.
% Assume f is 2 x m matrix with [x; y]
m = size(f, 2); % Number of points
% Define fractions and labels
fractions = [0, 0.25, 0.5, 0.75, 1];
labels = {'initial', '25%%', '50%%', '75%%', 'final'};
alignments = {'left', 'center', 'center', 'center', 'right'};
% Loop through and label each point
for i = 1:length(fractions)
idx = round(1 + (m - 1) * fractions(i));
text( f(1,idx), f(2,idx), ...
sprintf('%s - %.2f', labels{i}, f(2,idx)), ...
'HorizontalAlignment', alignments{i} );
end
This looks more concise and professional.
Hope this helps!
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!