keeping elements of certain indices of an array

조회 수: 16 (최근 30일)
mikasa
mikasa 2014년 5월 19일
댓글: mikasa 2014년 5월 20일
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