remove elements...and save in another vector...

2 次查看(过去 30 天)
I have column vector like this P=[ 0;455.444; 326.416; 333.008; 200.806; 273.315; 267.822; 268.066;0 ; 457.153; 206.177; 282.349; 0; 450.806; 187.378; 265.869; 264.648; 266.357; 0; 444.336; 331.177; 333.374; 204.224; 255.981; 0 ;452.271; 322.388; 325.562; 193.726; 276.123; 0; 442.749; 178.955; 260.376; 0; and so on....]; Its corresponding T = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15; 16; 17; 18; 19; 20; 21; 22; 23; 24; 25; 26; 27; 28; 29; 30; 31; 32; 33; 34 ; and so on....];
I want to keep only 1st element from 0 element and last two non zero element before another O element (shown in bold). So LENGTH BETWEEN TWO 0 (the left 0 and right 0) is 3. Save 2nd position elements in P2 column vector and 3rd position elements in P3 column vector. 3rd position element in P4, 5th element in P5. The result should be
After extraction, the remaining elements should be placed into P1 such as
P1 = P=[ 0;455.444; 267.822; 268.066;0 ; 457.153; 206.177; 282.349; 0; * 450.806; 264.648; 266.357; 0; 444.336; 204.224; 255.981; 0 ;452.271; 193.726; 276.123; 0; 442.749; 178.955; 260.376; 0; and so on....]; P2= [ 326.416;206.177; 187.378; 331.177;322.388]; P3=[333.008;265.869; 331.177] P4=[200.806]; P5=[273.315]; and it corresponding T in T1; T2;T3;T3;T5
i tried using Pzeros = find(P == 0); violators = Pzeros(find(Pzeros(2 : end) - Pzeros(1: end - 1) > 4)); P1 = P; P1([violators + 2; violators + 3; violators + 4; violators + 5]) = []; P2 = P(violators + 2); P3 = P(violators + 3); P4 = P(violators + 4); P5 = P(violators + 5);
but facing some problem
  2 个评论
Sagar Dhage
Sagar Dhage 2014-8-12
Hi..In that code, i am not getting P = [P=[ 0; 212; 111; 190; 0; 223; 123; 200; 0; 190; 167; 181] after removing P2 and P3..How to get this modified P? Suppose P have corresponding T vector also. How to find corresponding T2, T3 for P2 and P3? also If Area=3, its not considering elements in P1,P2,P3 vector..

请先登录,再进行评论。

回答(1 个)

dpb
dpb 2014-8-11
Not fully developed, but how about working from the following...
>> pz=reshape(find(P==0),2,[]).'
pz =
1 9
13 19
25 31
>> cell2mat(arrayfun(@(ix,iy) [P(ix:ix+1);P(iy-2:iy-1)],pz(:,1),pz(:,2),'uniform',0))
ans =
0
455.4440
267.8220
268.0660
0
450.8060
264.6480
266.3570
0
452.2710
193.7260
276.1230
>>
  2 个评论
Sagar Dhage
Sagar Dhage 2014-8-12
its not including the elements which are in 0 of length is 3. i want keep those elements whose is 3 as well so i want ans like this
0
455.4440
267.8220
268.0660
0
457.153
206.177
282.349
0
450.8060
264.6480
266.3570
0
444.336
204.224
255.981
0
452.2710
193.7260
276.1230
0
442.749
178.955
260.376
0
dpb
dpb 2014-8-12
I quote from my initial posting -- "Not fully developed". A quick example to get the creative juices flowing...

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by