How to save a variable length vector iterating in a loop into an Array
Afficher commentaires plus anciens
Hi,
I was trying to do some detection using the code below
% generate total area
zone_dev=zeros(ndev);
area_zone=ndev/100;
% dev_zone=reshape(dev_num,[],area_zone);
% depth_dev_zone=reshape(depth,[],area_zone);
zone=1;
while(zone<=(ndev/100))
% cood_zone(1,zone)=round(mean(dev_zone(:,zone)))+10*randi(zone)
found=dev(dev<zone*100 & dev>(zone-1)*100))'
% if zone==0
% zone_dev_zone=[found]
% else
% zone_dev_zone=[zone_dev_zone,found]
% end
zone=zone+1;
end
Msr
However, as the variable found is a column vector of random size, found is overridden with the current loop values.
Can someone guide me further in saving these values into an Array/List/Table.
Thanks in Advance :)
Ayan
Réponses (1)
Geoff Hayes
le 24 Juin 2017
Modifié(e) : Geoff Hayes
le 24 Juin 2017
Ayan - if you want to save your found elements into an array, just initialize an array (before the while loop) to something that is sized appropriately. In your case, since you iterate from one to ndev/100, you will want to create a cell array with ndev/100 elements as
foundData = cell(ndev/100, 1);
zone = 1;
while(zone<=(ndev/100))
foundData{zone} = dev(dev<zone*100 & dev>(zone-1)*100))';
zone = zone + 1;
% etc.
end
By the way, you could probably use a for loop instead of a while loop since (in this case) both would be doing the same thing
for zone = 1:ndev/100
foundData{zone} = dev(dev<zone*100 & dev>(zone-1)*100))';
end
and you can then avoid using the zone = zone + 1 to increment zone since the for loop would handle this automatically for you.
1 commentaire
Ayyangar Narasimha
le 25 Juin 2017
Modifié(e) : Ayyangar Narasimha
le 25 Juin 2017
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!