summaryrefslogtreecommitdiff
path: root/macros/circshift.sci
diff options
context:
space:
mode:
Diffstat (limited to 'macros/circshift.sci')
-rw-r--r--macros/circshift.sci49
1 files changed, 49 insertions, 0 deletions
diff --git a/macros/circshift.sci b/macros/circshift.sci
new file mode 100644
index 0000000..3717cd3
--- /dev/null
+++ b/macros/circshift.sci
@@ -0,0 +1,49 @@
+
+function R = circshift(M,d)
+
+// Shifts array circularly
+
+// CALLING SEQUENCES:
+// R = circshift(M, d)
+// circularly shifts by d(i) positions components of M along its #ith dimensions
+
+// PARAMETERS:
+// M,R : vector or matrix of any data type
+// d : vector of integers. d(i) is the shift to be applied to the M's components
+// along its ith dimension.
+// for example d = [0 n] will shift element n position along column
+
+// EXAMPLES:
+// M = [1 2 3 4];
+// circshift(M, [0 1])
+
+//Output :
+// ans =
+//
+// 4. 1. 2. 3.
+
+
+
+ if argn(2)==0
+ error("Invalid input")
+ R = []
+ return
+ end
+ s = size(M)
+ R = M
+ for i=1:length(d)
+ if s(i)>1
+ D = pmodulo(d(i),s(i))
+ if D~=0
+ S = emptystr(1,length(s))+":"
+ S(i) = "[s(i)-D+1:s(i) 1:s(i)-D]"
+ S = strcat(S,",")
+ if typeof(R) ~= "ce"
+ execstr("R = R("+S+")")
+ else
+ execstr("R.entries = R("+S+").entries")
+ end
+ end
+ end
+ end
+endfunction