Filter löschen
Filter löschen

Circular shifting or rotating structure of array elements

7 Ansichten (letzte 30 Tage)
NALLARASU KRISH
NALLARASU KRISH am 4 Mär. 2024
Kommentiert: Voss am 5 Mär. 2024
I have a structure of array of size 1x3 containing x and y coordinates as follows.
4 8
20 24
5 15
I want to shift the third row to first one. later 2nd one to first row. How to do it by using circshift?
  1 Kommentar
Dyuman Joshi
Dyuman Joshi am 4 Mär. 2024
You should experiment with it, and see what results you get.
Refer to the documentation for more information and examples.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Voss
Voss am 4 Mär. 2024
A 1x3 array of structures:
S = struct('x',{4 20 5},'y',{8 24 15})
S = 1×3 struct array with fields:
x y
S.x
ans = 4
ans = 20
ans = 5
S.y
ans = 8
ans = 24
ans = 15
1. Shift the third element to first one using circshift:
S = circshift(S,1);
S.x
ans = 5
ans = 4
ans = 20
S.y
ans = 15
ans = 8
ans = 24
2. Later 2nd one to first element using circshift:
S = circshift(S,-1);
S.x
ans = 4
ans = 20
ans = 5
S.y
ans = 8
ans = 24
ans = 15
  2 Kommentare
NALLARASU KRISH
NALLARASU KRISH am 5 Mär. 2024
I apreciate your approach in explaining array of structures and structure of arrays. Positive sign means shifting downwards and vice versa. Numerical value denotes the number of times the shift to occur. Thank you!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

Hassaan
Hassaan am 4 Mär. 2024
Bearbeitet: Hassaan am 4 Mär. 2024
% Shift rows up by one position, making the third row the first
A_shifted = circshift(A, -1);
where A is your matrix/array.
Reference
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
  1 Kommentar
NALLARASU KRISH
NALLARASU KRISH am 4 Mär. 2024
Thank you! Your approach works on arrays and matrices, but not on structure of arrays!

Melden Sie sich an, um zu kommentieren.


Sam Chak
Sam Chak am 4 Mär. 2024
Me, first time using circshift(). So, I'm not entirely sure about your intention. Are you suggesting to swap the 3rd row with the 1st row? And then, do you want to swap the 2nd row with the newly positioned 1st row?
Please clarify if my understanding is correct.
A = [ 4 8 % 1st row
20 24 % 2nd row
5 15]; % 3rd row
A([1,3],:) = circshift(A([1,3],:), 1)
A = 3×2
5 15 20 24 4 8
A([1,2],:) = circshift(A([1,2],:), 1)
A = 3×2
20 24 5 15 4 8

Voss
Voss am 4 Mär. 2024
Bearbeitet: Voss am 4 Mär. 2024
A structure of arrays:
S = struct('x',[4;20;5],'y',[8;24;15])
S = struct with fields:
x: [3×1 double] y: [3×1 double]
S.x,S.y
ans = 3×1
4 20 5
ans = 3×1
8 24 15
1. Shift the third row to first one using circshift:
S = structfun(@(v)circshift(v,1),S,'uni',0);
S.x,S.y
ans = 3×1
5 4 20
ans = 3×1
15 8 24
2. Later 2nd one to first row using circshift:
S = structfun(@(v)circshift(v,-1),S,'uni',0);
S.x,S.y
ans = 3×1
4 20 5
ans = 3×1
8 24 15

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by