Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Matrix manipulation problem

1 view
Skip to first unread message

Milos Milenkovic

unread,
Feb 11, 2012, 5:39:09 AM2/11/12
to
Dear,
if we have a matrix
A=[0 2 4 2 3 4; 3 4 6 4 7 8]
how to make a matrix (to extract from each row value number 1,3,4,6,7,9.... and form Aprime?
Aprime=[0 4 2 4; 3 6 4 8]

Best,
Milos

Bruno Luong

unread,
Feb 11, 2012, 6:04:12 AM2/11/12
to
"Milos Milenkovic" <m.mile...@mathworks.com> wrote in message <jh5ggd$p4o$1...@newscl01ah.mathworks.com>...
A(:,[1 3 4 6])

Bruno

Milos Milenkovic

unread,
Feb 11, 2012, 9:59:15 AM2/11/12
to
"Bruno Luong" <b.l...@fogale.findmycountry> wrote in message <jh5hvc$t59$1...@newscl01ah.mathworks.com>...
Thanks Bruno,
but I need a solution for a common case where there are matrices of different dimensions. I think that this A(:,[1 3 4 6 7 9 .....]) will not be of use.
Best

dpb

unread,
Feb 11, 2012, 10:06:50 AM2/11/12
to
Well, it would surely seem to be the hint on how to construct the
indices required.

We can't read your mind and know what situations you're looking at w/o
being told that.

Build a vector based on the rule(s) for selecting the desired indices
and limit it by the appropriate lengths. It could require wrapping the
code/logic into a function rather than a single-line solution if those
rules can't be written trivially.

--

Bruno Luong

unread,
Feb 11, 2012, 10:39:19 AM2/11/12
to
"Milos Milenkovic" <m.mile...@mathworks.com> wrote in message <jh5vo3$8b0$1...@newscl01ah.mathworks.com>...
See if I can read your mind
A(:,2:3:end) = []

% Bruno

Milos Milenkovic

unread,
Feb 11, 2012, 10:43:10 AM2/11/12
to
dpb <no...@non.net> wrote in message <jh6066$bmm$1...@speranza.aioe.org>...
Dear dbm,
yes, you are wright I didn't formulate problem properly.
Let's say that from every matrix on every three columns (matrices are always n*3), I have to select first and third and from them to form a separate matrix.
So if I have matrix of nine columns, the new matrix will be composed from 1,3,4,6,7,9 column.
Sorry!
Best

dpb

unread,
Feb 11, 2012, 1:40:52 PM2/11/12
to
On 2/11/2012 9:43 AM, Milos Milenkovic wrote:
...

> from every matrix on every three columns (matrices are always n*3), I
> have to select first and third and from them to form a separate matrix.
> So if I have matrix of nine columns, the new matrix will be composed
> from 1,3,4,6,7,9 column. Sorry!

I still don't follow the general rule here.

You say a n*3 column matrix (presuming) n=1,2,3,... and want every three
(third?) column. That would be seemingly be x(:,[1:3:end]} which for
n=2 --> [1 4] or n=3 --> [1 4 7] but you throw in 3,6, and 9 as well for
the n=3 case.

What the above looks like is starting w/ 1:2:n except after the first
two you then increment the starting point by one and repeat...

What's actual precise definition of the rule?

--

Roger Stafford

unread,
Feb 11, 2012, 1:46:12 PM2/11/12
to
"Milos Milenkovic" <m.mile...@mathworks.com> wrote in message <jh62ae$fi5$1...@newscl01ah.mathworks.com>...
> Let's say that from every matrix on every three columns (matrices are always n*3), I have to select first and third and from them to form a separate matrix.
> So if I have matrix of nine columns, the new matrix will be composed from 1,3,4,6,7,9 column.
- - - - - - - -
Bruno's method is shorter in this case of course, but in general whenever you have two alternating sizes of index increments, you can always generate the indices with appropriate linear combinations of a) 1:n, b) mod(1:n,2), and c) a constant, (where n is the total number to be selected from.) In this case it would be:

B = A(:,(3*(1:n)-mod(1:n,2))/2);

Roger Stafford

Milos Milenkovic

unread,
Feb 11, 2012, 1:56:09 PM2/11/12
to
"Roger Stafford" wrote in message <jh6d1k$g35$1...@newscl01ah.mathworks.com>...
Dear,
this is solution:
for any matrix A of 3*n dimensions =>
A1=A(:,1:3:end)
A2=A(:,2:2:end)
B(:,1:2:end) =A1
B(:,2:2:end)=A2
So I will get a matrix composed from first, third, fourth.... column of A.
Thanks Roger!
0 new messages