'How to' Matrix
정보
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
이전 댓글 표시
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
Walter Roberson
2012년 12월 12일
What if there is a 1 in the first 4 columns?
Also, should the new matrix have fewer columns?
TAB
2012년 12월 12일
Then what should be left in 5th column (or row in your words) ?
Jan
2012년 12월 12일
@Student: The question remains, what "raise to the first row" exactly means. At first I guess it is the "first column", but "raising" can be swapping, shifting, circular shifting, sorting, cropping or any procedure I cannot imagine currently.
Hello kity
2012년 12월 12일
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일
0 개 추천
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
Hello kity
2012년 12월 12일
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
Hello kity
2012년 12월 12일
Some ideas:
x = [0, 0, 0, 0, 1];
index = find(x, 1, 'first');
y1 = x(index:end);
y2 = [x(index:end), zeros(1, index - 1)];
y3 = [x(index:end), ones(1, index - 1)];
y4 = [x(index:end), repmat(x(end), 1, index - 1)];
y5 = [x(index:end), x(1:index - 1)];
댓글 수: 0
이 질문은 마감되었습니다.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!