Help with jumping one position using circshift function in a for loop

3 次查看(过去 30 天)
Hi there,
I am trying to create a transformation matrix for the matrix stiffness method, and to save time I am using the circshift function in a for loop.
So, I am starting off with this
n = 12;
T = zeros(2,n);
T(1,1) = 1;
T(2,2) = 1
for i = 1:9
Tn(:,:,i) = circshift(T,[0,i])
end
However, every 3rd interation I want the values (the 1's) to shift one extra place to the right. So I want the Tn matrix to follow this pattern:
Tn = [1 1 0 1 1 0 1 1 0 1 1 0;
0 1 1 0 1 1 0 1 1 0 1 1]
I guessed at trying this:
for i = 1:9
Tn(:,:,i) = circshift(T,[0,i])
if i == 3,6,9
Tn(:,:,i) = circshift(T,[0,i+1])
endif
end
But it didn't work.
I hope I have explained this clearly. Can somebody help please?
Many thanks.
  1 个评论
Chhayank Srivastava
Could you clarify what do you mean when you say you want Tn to follow that pattern, does that mean after running the loop Tn should look like that?

请先登录,再进行评论。

采纳的回答

Star Strider
Star Strider 2023-8-15
The circshift function does not duplicate any values, so I don’t understand how you expect to get the ‘Tn’ matrix at the end.
I’m not certain what result you want otherwise.
Try this —
n = 12;
T = zeros(2,n);
T(1,1) = 1;
T(2,2) = 1
T = 2×12
1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
si = 0;
for i = 1:9
sia = rem(i,3) == 0;
si = [si+1+sia] % Shift Increments
Tn(:,:,i) = circshift(T,[0,si]);
end
si = 1
si = 2
si = 4
si = 5
si = 6
si = 8
si = 9
si = 10
si = 12
Tn
Tn =
Tn(:,:,1) = 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 Tn(:,:,2) = 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 Tn(:,:,3) = 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 Tn(:,:,4) = 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 Tn(:,:,5) = 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 Tn(:,:,6) = 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 Tn(:,:,7) = 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 Tn(:,:,8) = 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 Tn(:,:,9) = 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
Ths ‘si’ values are the increments provided to circshift so that you can keep track of them. (Suppress that line’s output when its display is no longer necessary.)
.

更多回答(3 个)

Voss
Voss 2023-8-15
编辑:Voss 2023-8-17
n = 12;
T = zeros(2,n);
T(1,1) = 1;
T(2,2) = 1
T = 2×12
1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
Here's one way to generate that Tn from this T using circshift in a loop:
Tn = zeros(2,n,8);
shift = 0;
for i = 1:size(Tn,3)
if mod(i,2) == 1
shift = shift+1;
end
Tn(:,:,i) = circshift(T,[0,shift-1]);
shift = shift+1;
end
disp(Tn);
(:,:,1) = 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 (:,:,2) = 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 (:,:,3) = 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 (:,:,4) = 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 (:,:,5) = 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 (:,:,6) = 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 (:,:,7) = 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 (:,:,8) = 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1

Chhayank Srivastava
Hi,
I see some issue with the if statement mentioned above.
So just fixing the if statement
n = 12;
T = zeros(2,n);
T(1,1) = 1;
T(2,2) = 1;
for i = 1:9
Tn(:,:,i) = circshift(T,[0,i]);
if mod(i,3) == 0
Tn(:,:,i) = circshift(T,[0,i+1]);
end
end
Tn
Tn =
Tn(:,:,1) = 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 Tn(:,:,2) = 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 Tn(:,:,3) = 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 Tn(:,:,4) = 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 Tn(:,:,5) = 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 Tn(:,:,6) = 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 Tn(:,:,7) = 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 Tn(:,:,8) = 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 Tn(:,:,9) = 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1
Moreover, I am assuming after running the program you want the Tn to look like
Tn = [1 1 0 1 1 0 1 1 0 1 1 0;
0 1 1 0 1 1 0 1 1 0 1 1];
But in the above code you are generating a 3D matrix.
A simpler solution would be just to use repmat and transformation matrix
T = [1,1,0;0,1,1];
Tn = repmat(T,1,4)
Tn = 2×12
1 1 0 1 1 0 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 0 1 1
But, going by your method
clear;clc;
n = 12;
T = zeros(2,n);
T(1,1) = 1;
T(2,2) = 1;
for i = 1:9
if mod(i,2) == 0
T = circshift(T,[0,2]);
Tn(:,:,i) = T;
else
T = circshift(T,[0,1]);
Tn(:,:,i) = T;
end
end
Tn = Tn(:,:,1)+Tn(:,:,2)+Tn(:,:,3)+Tn(:,:,4)+Tn(:,:,5)+Tn(:,:,6)+Tn(:,:,7)+Tn(:,:,8)
Tn = 2×12
1 1 0 1 1 0 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 0 1 1

Scott Banks
Scott Banks 2023-8-15
Hi guys,
thank you for your repsonses.
Sorry for the confusion for the final Tn matrix. It was a poor way of getting across my idea.
Really, it should look like how Star Strider answered. Which was 9 different matrices in the form of:
Tn =
Tn(:,:,1) =
0 1 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0
Tn(:,:,2) =
0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0
Tn(:,:,3) =
0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0 0
Tn(:,:,4) =
0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0 0
Tn(:,:,5) =
0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0
Tn(:,:,6) =
0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0
Tn(:,:,7) =
0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0
Tn(:,:,8) =
0 0 0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0
Tn(:,:,9) =
0 0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 0 0 1
  5 个评论
Voss
Voss 2023-8-17
@Scott Banks: I'm a little confused because if Tn(:,:,3) and T(n:,:,4) are both shifted to the right, then they are still the same as each other, which contradicts your statement that they should not be the same as each other.
Can you just write down the complete 3D array as it should be?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by