Relocate element from a row to another

조회 수: 1 (최근 30일)
Giannakis Stoukas
Giannakis Stoukas 2015년 3월 21일
편집: dpb 2015년 3월 22일
Hello,i want to relocate an element from a row to an another and delete the other row.For example i have the matrix
A=[1 2 4 8 3 1;
1 9 7 6 1 ;
1 5 1 ]
and i want to relocate the 5 so i will have the matrix
A=[1 2 4 8 3 1;
1 9 7 5 6 1]
  댓글 수: 7
the cyclist
the cyclist 2015년 3월 22일
It's not possible to understand the rule you want to follow, from the one example you give.
Giannakis Stoukas
Giannakis Stoukas 2015년 3월 22일
Actually it is a problem of local search algorithm,and it is too complicated it explain it from here. I just wanted the orders to move the element 5 between 7 and 6,and move all the other elements one position right.The rule to delete a row is "if the row has only zeros or ones,delete it",and i want the orders to do it.I know i am not very comprehensible,but it is a bit complicated to explain all the theory.

댓글을 달려면 로그인하십시오.

답변 (2개)

the cyclist
the cyclist 2015년 3월 22일
A=[1 2 4 8 3 1;
1 9 7 6 1 0;
1 5 1 0 0 0]
A = [A(1,:);
A(2,1:3) A(3,2) A(2,4:5)]
This is my best guess at what you want.

dpb
dpb 2015년 3월 22일
편집: dpb 2015년 3월 22일
Well, it's not possible to write anything except the specific answer without a definition of the logic. Anything else is indiscernible from magic, not science.
But, let's see, addressing the question as asked...
>> id76=sub2ind(size(A'),4,2);
>> id5=sub2ind(size(A'),2,3);
>> B=reshape(A',1,[]);
>> B=reshape([B(1:id76-1) B(id5) B(id76:id5-1) B(id5+1:end)],size(A,2),[]).';
>> B(all(B==0|B==1,2),:)=[]
B =
1 2 4 8 3 1
1 9 7 5 6 1
>>
Needless to say, to make this of much use in general you'll have to have rules to locate the magic numbers used to compute the indices id5 and id76

카테고리

Help CenterFile Exchange에서 Data Preprocessing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by