I have two vectors A and B. A contains zeros and ones, whereas B contains the vectors at which A is zero or 1. I want to know the range of B where A is 1

2 visualizzazioni (ultimi 30 giorni)
for eg.
A=[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 1 1]
B=[0.0359624956468314 0.143849982587326 0.323662460821483 0.575399930349303 0.899062391170786 1.29464984328593 1.76216228669474 2.30159972139721 2.91296214739335 3.59624956468314 4.35146197326660 5.17859937314373 6.07766176431451 7.04864914677896 8.09156152053707 9.20639888558885 10.3931612419343 11.6518485895734 12.9824609285061 14.3849982587326 15.8594605802527 17.4058478930664 19.0241601971738 20.7143974925749 22.4765597792696 24.3106470572581 26.2166593265401 28.1945965871158 30.2444588389852 32.3662460821483 34.5599583166050 36.8255955423554 39.1631577593994 41.5726449677371 44.0540571673685 46.6073943582935 49.2326565405122 51.9298437140246 54.6989558788306 57.5399930349303 60.4529551823236 63.4378423210106 66.4946544509913 69.6233915722656 72.8240536848336 76.0966407886953 79.4411528838506 82.8575899702996 86.3459520480423 89.9062391170786 93.5384511774085 97.2425882290322 101.018650271949 104.866637306160 108.786549331665 112.778386348463 116.842148356555 120.977835355941 125.185447346620 129.464984328593 133.816446301860 138.239833266420 142.735145222274 147.302382169422 151.941544107863 156.652631037598 161.435642958626 166.290579870949 171.217441774564 176.216228669474 ]
How to find the range of B where A is 1
The answer should come as
range1=[19.02 36.83 ]
range2=[101 147.3]
range3=[171.2 176.2]

Risposta accettata

Stephan
Stephan il 26 Mar 2019
Modificato: Stephan il 26 Mar 2019
result = range(B(A==1))
You edited the question - here's the edited answer:
% your arrays
A=[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 1 1];
B=[0.0359624956468314 0.143849982587326 0.323662460821483 0.575399930349303 0.899062391170786 1.29464984328593 1.76216228669474 2.30159972139721 2.91296214739335 3.59624956468314 4.35146197326660 5.17859937314373 6.07766176431451 7.04864914677896 8.09156152053707 9.20639888558885 10.3931612419343 11.6518485895734 12.9824609285061 14.3849982587326 15.8594605802527 17.4058478930664 19.0241601971738 20.7143974925749 22.4765597792696 24.3106470572581 26.2166593265401 28.1945965871158 30.2444588389852 32.3662460821483 34.5599583166050 36.8255955423554 39.1631577593994 41.5726449677371 44.0540571673685 46.6073943582935 49.2326565405122 51.9298437140246 54.6989558788306 57.5399930349303 60.4529551823236 63.4378423210106 66.4946544509913 69.6233915722656 72.8240536848336 76.0966407886953 79.4411528838506 82.8575899702996 86.3459520480423 89.9062391170786 93.5384511774085 97.2425882290322 101.018650271949 104.866637306160 108.786549331665 112.778386348463 116.842148356555 120.977835355941 125.185447346620 129.464984328593 133.816446301860 138.239833266420 142.735145222274 147.302382169422 151.941544107863 156.652631037598 161.435642958626 166.290579870949 171.217441774564 176.216228669474 ];
% call function
result = findRange(A,B)
function range = findRange(A,B)
% indices of A which are equal to 1
res = find(A==1);
% Starting index for a new block in res
idx_start = [1 find(diff(find(A~=0))~=1)+1];
% Ending index for blocks
idx_end = [find(diff(find(A~=0))~=1) numel(res)];
% preallocate
blocks = cell(1,numel(idx_start));
range = nan(numel(idx_start),2);
% Building blocks and evaluating range;
for k = 1:numel(idx_start)
blocks(k) = {B(res(idx_start(k):idx_end(k)))};
range(k,:) = [min(blocks{k}) max(blocks{k})];
end
end
result is:
result =
19.0242 36.8256
101.0187 147.3024
171.2174 176.2162
Best regards
Stephan
  4 Commenti
RAJAN PRASAD
RAJAN PRASAD il 26 Mar 2019
Thank you very much.The code works great. I would start the question in next thread from now on. My sincere apolgies for editing the question.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Resizing and Reshaping Matrices in Help Center e File Exchange

Prodotti


Release

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by