Circular shifting or rotating structure of array elements
9 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Dyuman Joshi
2024 年 3 月 4 日
You should experiment with it, and see what results you get.
Refer to the documentation for more information and examples.
採用された回答
Voss
2024 年 3 月 4 日
A 1x3 array of structures:
S = struct('x',{4 20 5},'y',{8 24 15})
S.x
S.y
1. Shift the third element to first one using circshift:
S = circshift(S,1);
S.x
S.y
2. Later 2nd one to first element using circshift:
S = circshift(S,-1);
S.x
S.y
その他の回答 (3 件)
Hassaan
2024 年 3 月 4 日
編集済み: Hassaan
2024 年 3 月 4 日
% 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.
Sam Chak
2024 年 3 月 4 日
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([1,2],:) = circshift(A([1,2],:), 1)
Voss
2024 年 3 月 4 日
編集済み: Voss
2024 年 3 月 4 日
A structure of arrays:
S = struct('x',[4;20;5],'y',[8;24;15])
S.x,S.y
1. Shift the third row to first one using circshift:
S = structfun(@(v)circshift(v,1),S,'uni',0);
S.x,S.y
2. Later 2nd one to first row using circshift:
S = structfun(@(v)circshift(v,-1),S,'uni',0);
S.x,S.y
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Manage Products についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!