Info
此问题已关闭。 请重新打开它进行编辑或回答。
'How to' Matrix
1 次查看(过去 30 天)
显示 更早的评论
I have an array with 0 s and 1s.
Example a colomn, 0 ,0 ,0 ,0 , 1.
Now the 1 is on the 5th row, I want to raise that to the first row.
So, in my code i want all the ones to go 4 steps up...
sounds easy, but how :) ?
5 个评论
Jan
2012-12-12
Thanks for the clarification, Student. And what happens on the right margin? Is the result shorter, filled with zeros or ones or with the value of the last element?
回答(4 个)
Walter Roberson
2012-12-12
编辑:Walter Roberson
2012-12-12
Have you considered circshift() up by 4 rows?
0 个评论
Azzi Abdelmalek
2012-12-12
A=[0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1]
for k=5:length(A)
if A(k)
b=A(k-4:k-1);
A(k-4)=1
A(k-4+1:k)=b
end
end
2 个评论
Azzi Abdelmalek
2012-12-12
A=[0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1]
for k=5:length(A)
if A(k) % If A(k) is equal to 1
b=A(k-4:k-1); % store value from k-4 to k-1 in b
A(k-4)=1 % replace value at k-1 by 1
A(k-4+1:k)=b % shift value stored in b
end
end
Andrei Bobrov
2012-12-12
编辑:Andrei Bobrov
2012-12-12
other way:
a - your vector - row (eg: [0 0 0 0 1])
n = numel(a);
out = a(hankel(1:n,[n,1:n-1]));
or
out = hankel(a,circshift(a,[0 1]));
1 个评论
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!