简体   繁体   English

使用circshift octave / matlab进行数组操作

[英]array manipulation with circshift octave / matlab

1) I'm trying to shift an array's (outer cells) first and last cell inward at the same time. 1)我试图先将一个数组(外部单元格)和最后一个单元格同时向内移动 Here's an inward animation of what I'm trying to do with the array inward animation . 这是我正在尝试用数组向内动画做的内部动画 As you can see the outer cells are moving inward (from the ends) at the same time 如您所见,外部细胞同时向内(从末端)移动

Here's a pic but the animation shows it much better please note the array could have either an even or odd amount of cells 这是一张照片,但动画显示它更好请注意阵列可能有一个偶数或奇数的单元格

Inner rotation steps
1 2 3 4 5 6 7
4 1 2 3 6 7 5
3 4 1 2 7 5 6

向内方向

2) I'm trying to shift an array's middle cells outward using circshift (which I think is fastest) at the same time. 2)我正在尝试使用circshift(我认为最快) 向外移动数组的中间单元格。 Here's an outward animation of what I'm trying to do also outward animation . 这是我正在尝试做的外向动画的向外动画 As you can see the middle of the signal is moving outward (left and right) at the same time. 如您所见,信号的中间同时向外(左右)移动。

Here's a pic but the animation shows it much better please note the array could have either an even or odd amount of cells 这是一张照片,但动画显示它更好请注意阵列可能有一个偶数或奇数的单元格

Outer rotation steps
1 2 3 4 5 6 7
2 3 4 1 7 5 6
3 4 1 2 6 5 7

在此输入图像描述

Example: inward
a = (1:7)
y=circshift(A,[0 -2]) %shift end of array inward
3   4   5   6   7   1   2

a = (1:7)
y=circshift(A,[0 2]) %shift beginning of array inward
6   7   1   2   3   4   5

Not to sure how to do the middle cells shifting outward using circshift or the outer cells shifting inward at the same time 不确定如何使用circshift向外移动中间细胞或同时向外移动细胞

I'm not sure about how to start circshift from the center and move the array outwards / inwards to get this effect. 我不确定如何从中心开始旋转并向外/向内移动阵列以获得此效果。

Please note I'm not trying to get this equation I'm just trying to get the arrays to move in the same way. 请注意我不是试图得到这个等式我只是想让阵列以相同的方式移动。 I'm using octave 3.8.1 which is compatible with matlab. 我正在使用与matlab兼容的octave 3.8.1。

A = 1:7;

split = ceil(numel(A)/2);

n = 2;
A(1:split) = circshift(A(1:split), [0, n]);
A(split+1:end) = circshift(A(split+1:end), [0, -n]);

Put the last three lines in a loop if you like. 如果您愿意,可以将最后三行放在循环中。 Also just change the signs of n for inwards or outwards 也只是向内或向外改变n的符号

what about constructing new indices instead of using circshift: 如何构建新索引而不是使用circshift:

A = 1:7;

halfLen = ceil(length(A)/2); % or use ceil to 
idcsOutward = [2:halfLen,1,length(A),(halfLen+1):(length(A)-1)];

B1 = A(idcsOutward)
B2 = B1(idcsOutward)

% and inward:
idcsInward = [halfLen,1:(halfLen-1),(halfLen+2):length(A),halfLen+1];

C1 = A(idcsInward)
C2 = C1(idcsInward)

Result is: 结果是:

B1 =
     2     3     4     1     7     5     6

B2 =
     3     4     1     2     6     7     5

C1 =
     4     1     2     3     6     7     5

C2 =
     3     4     1     2     7     5     6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM