Help with jumping one position using circshift function in a for loop
조회 수: 2 (최근 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
2023년 8월 15일
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
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
si = 0;
for i = 1:9
sia = rem(i,3) == 0;
si = [si+1+sia] % Shift Increments
Tn(:,:,i) = circshift(T,[0,si]);
end
Tn
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.)
.
댓글 수: 0
추가 답변 (3개)
Voss
2023년 8월 15일
편집: Voss
2023년 8월 17일
n = 12;
T = zeros(2,n);
T(1,1) = 1;
T(2,2) = 1
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);
댓글 수: 0
Chhayank Srivastava
2023년 8월 15일
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
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)
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)
댓글 수: 0
Scott Banks
2023년 8월 15일
댓글 수: 5
Star Strider
2023년 8월 17일
@Scott Banks — Thank you!
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?
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!