How to save a variable length vector iterating in a loop into an Array

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
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
Ayyangar Narasimha le 25 Juin 2017
Modifié(e) : Ayyangar Narasimha le 25 Juin 2017

Hi Geoff, I was trying to achive something similar, Can't think of a way to make it simpler, any help is appreciated.

if true
  % % 
%
%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))
   switch zone
      case 1
          found_1=(dev(dev<=zone*100 & dev>(zone-1)*100))
          [ln_1,nl_1]=intersect(dev,found_1)
          dev_1=sort(nonzeros(nl_1))
          if intersect(Msr,dev_1)==Msr
              Msr_zone=zone
          end
      case 2
          found_2=(dev(dev<=zone*100 & dev>(zone-1)*100))
          [ln_2,nl_2]=intersect(dev,found_2)
          dev_2=sort(nonzeros(nl_2))
          if intersect(Msr,dev_2)==Msr
              Msr_zone=zone
          end
      case 3
          found_3=(dev(dev<=zone*100 & dev>(zone-1)*100))
          [ln_3,nl_3]=intersect(dev,found_3)
          dev_3=sort(nonzeros(nl_3))
          if intersect(Msr,dev_3)==Msr
              Msr_zone=zone
          end
      case 4
          found_4=(dev(dev<=zone*100 & dev>(zone-1)*100))
          [ln_4,nl_4]=intersect(dev,found_4)
          dev_4=sort(nonzeros(nl_4))
          if intersect(Msr,dev_4)==Msr
              Msr_zone=zone
          end
      case 5
          found_5=(dev(dev<=zone*100 & dev>(zone-1)*100))
          [ln_5,nl_5]=intersect(dev,found_5)
          dev_5=sort(nonzeros(nl_5))
          if intersect(Msr,dev_5)==Msr
              Msr_zone=zone
          end
          found_ol=found
    end
    zone=zone+1
end
Msr
end

Thanks ..:)

Ayan

Connectez-vous pour commenter.

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!

Translated by