# keeping elements of certain indices of an array

88 views (last 30 days)
mikasa on 19 May 2014
Commented: mikasa on 20 May 2014
I have a B column(Nx1) and i want to keep the elements of these indices: 11:2:41 and 45:4:101 and 111:10:401 and 441:40:801 and 901 and 1001.
So i want to 'extract' 73 elements of this B column to a new one D column, so that B(11)=D(1), B(13)=D(2),.....B(1001)=D(73).
As I've searched, matlab can't do looping with two parallel conditions, so I could write something like:
>>for i=11:2:41 and j=1:16
B(i)=D(j)
end...
So i have to connect i and j to one variable k, and loop for k..
The only idea i had is to write a BA table of 2 columns like this:
1 11
2 13
3 15
....
73 1001
an then write
>> for i=1:length(BA)
ba1=BA(i,1);ba2=BA(i,2) %%so that ba1=1 and ba2=11
B(ba2)=D(ba1) %%%so that B(11)=D(1)
end..
But this comes with this error: Undefined function 'D' for input arguments of type 'double'.
Also if i try to just erase elements with [], its getting very time consuming because if i want to keep indices e.g. 45,49,53 then i ll have to erase for 46:48,50:52, 54:56 etc....

Cedric Wannaz on 19 May 2014
Edited: Cedric Wannaz on 19 May 2014
The simplest way is probably as follows:
indices = [11:2:41, 45:4:101, 111:10:401, 441:40:801, 901, 1001] ;
D = B(indices) ;
PS: it was a good start and trying several approaches is good too, it will pay ultimately!
mikasa on 20 May 2014
Thank you so much! It works perfect!
P.S. I hope it pays off soon because I've spent plenty of hours on '2-line solution' problems, just like this!!