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
回答(1 个)
Geoff Hayes
2017-6-24
编辑:Geoff Hayes
2017-6-24
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 个评论
Ayyangar Narasimha
2017-6-25
编辑:Ayyangar Narasimha
2017-6-25
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!