keeping elements of certain indices of an array

18 次查看(过去 30 天)
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....
Please help!! Thank you!

采纳的回答

Cedric
Cedric 2014-5-19
编辑:Cedric 2014-5-19
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!
  1 个评论
mikasa
mikasa 2014-5-20
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!!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by