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!