Tony YEe
Tony YEe

Reputation: 235

How to extract the sequence matrix elements?

I have a matrix as shown in below:

A=[2;1;8;5;4;7]

now i need to extract the matrix A into 2 parts:

newpoint=[2];
rest=[1;8;5;4;7];

then apply loop again to extract the second column as new point :

newpoint=[1];
rest=[2;8;5;4;7];

Applying loop again to take third column number as new point :

newpoint=[8];
rest=[2;1;5;4;7];

Take the number in row sequence until the last row . Can someone be kind enough to help.Thanks!

Upvotes: 0

Views: 101

Answers (2)

sfotiadis
sfotiadis

Reputation: 977

Something like that might do:

for i=1:length(A)    
    newpoint = A(i);
    if i==1
    rest = A(i+1:end);
    else
    if i== length(A);
    rest = A(1:end-1);
    else
    rest=A(1:i-1,i+1:end);

    ... stuff to do
end

Upvotes: 1

ThijsW
ThijsW

Reputation: 2599

I would go for something like this:

for i = 1:size(A,1)
    newpoint = A(i,1)
    rest = A;
    rest(i) = [];
    %# use rest and newpoint
end

Or if you prefer saving all the rest and newpoints in a matrix:

newpoint = zeros(size(A,1),1);
rest = zeros(size(A,1)-1,size(A,1));
for i = 1:size(A,1)
    newpoint(i) = A(i,1);
    temp = A;
    temp(i) = [];
    rest(:,i) = temp;
end

Upvotes: 1

Related Questions