Relocate element from a row to another
Show older comments
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 Comments
dpb
on 21 Mar 2015
Neither the beginning nor the ending A are legal; best you could do would be a cell array.
The "want" is also indeterminate...what's the logic for deciding what's the end result; without some rule best you can do is simply create it as desired.
Giannakis Stoukas
on 22 Mar 2015
Edited: per isakson
on 22 Mar 2015
per isakson
on 22 Mar 2015
I don't get why the "5" ends up between the "7" and the "6".
dpb
on 22 Mar 2015
Because he said so...
Giannakis Stoukas
on 22 Mar 2015
the cyclist
on 22 Mar 2015
It's not possible to understand the rule you want to follow, from the one example you give.
Giannakis Stoukas
on 22 Mar 2015
Answers (2)
the cyclist
on 22 Mar 2015
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.
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
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!