How to flip an ordered X-axis that is half half separated for data?

조회 수: 2 (최근 30일)
Dorna
Dorna 2023년 6월 3일
편집: Star Strider 2023년 6월 4일
Hi, I have a plot that I want its X-axis to be reversed. My data on X-axis is showing as
[1 2 3 4 5 6 7 8 9 10 11 12] I want to reverse it in a way that it represents [6 5 4 3 2 1 12 11 10 9 8 7] (reversing it from the middle for two seperate parts). How is it possible? I tried set(gca, 'XDir','reverse') but it reverses it to [12 11 10 9 8 7 6 5 4 3 2 1]. I cannot share the code or data due to confidentiality. Thanks in advance.

채택된 답변

Dorna
Dorna 2023년 6월 4일
Ok, so what I needed to do was
A = Data((1:20),(1:5))
B = flip(A,1)
C = Data((21:40),(1:5))
D = flip(C,1)
And then
New = cat(1,B,D)
  댓글 수: 1
Star Strider
Star Strider 2023년 6월 4일
편집: Star Strider 2023년 6월 4일
O.K.
That seems to be essentially what I wrote, although in one column not five. You can use my approach to plot it.

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

추가 답변 (2개)

VBBV
VBBV 2023년 6월 3일
Use xticklabels function to modify the order of axis labels
x1 = {'6','5','4','3','2','1'};
x2 ={'12','11','10','9','8','7'};
plot(rand(1,12))
xticks(1:12)
xticklabels([x1,x2])
  댓글 수: 2
Dorna
Dorna 2023년 6월 3일
@VBBV will also flip the data? I think it will only flip the axis label. Plus i prefer something that does not require me to actually work with the plots as they are very complex. Something like set gca
VBBV
VBBV 2023년 6월 3일
편집: VBBV 2023년 6월 3일
can you explain why you DONT want to actually work with plot data but still want to flip ordered x-axis values ?
v = [16 5 9 4 2 11 7 14];
v2 = v([5:8 1:4]) % Extract and swap the halves of v
v2 = 1×8
2 11 7 14 16 5 9 4
% flip the values for vector v
v2 = flip(v)
v2 = 1×8
14 7 11 2 4 9 5 16
% another way
v2 = v([8:-1:5 , 4:-1:1])
v2 = 1×8
14 7 11 2 4 9 5 16
How can I make the V2 to be [14 7 11 2 4 9 5 16], can I use prime on 5:8'?
You can use the flip function to get the desired result as shown above

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


Star Strider
Star Strider 2023년 6월 3일
One approach that changes the x-axis and the data —
xv = [1 2 3 4 5 6 7 8 9 10 11 12]; % Independent Variable
yv = (xv/max(xv)).^2; % Dependent Variable
xvi = [6 5 4 3 2 1 12 11 10 9 8 7]; % Indexing Vector
figure
plot(xv, yv)
grid
xticks(xv)
title('Original')
Vectors = [xv(xvi); yv(xvi)]
Vectors = 2×12
6.0000 5.0000 4.0000 3.0000 2.0000 1.0000 12.0000 11.0000 10.0000 9.0000 8.0000 7.0000 0.2500 0.1736 0.1111 0.0625 0.0278 0.0069 1.0000 0.8403 0.6944 0.5625 0.4444 0.3403
figure
plot(xv, yv(xvi))
grid
xticks(xv)
xticklabels(xv(xvi))
title('Reversed & Concatenated')
It may be challenging to elliminate the connecting line between 1 and 12 here. If necessary, that would require putting a NaN value at the beginning or end of the ‘xv’ and ‘yv’ vectors, and adjusting ‘xvi’ accordingly.
.
  댓글 수: 4
Dorna
Dorna 2023년 6월 4일
@Star Strider thank you so much. I think what I needed was what I posted as answer but I do really appreciate your input.
Dorna
Dorna 2023년 6월 4일
I wish there was an easier alternative to the solution that I posted.

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by