Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

For loop Issue, Loops, Row Col Indexing Issue

3 vues (au cours des 30 derniers jours)
rizwan
rizwan le 7 Avr 2015
Clôturé : MATLAB Answer Bot le 20 Août 2021
Experts, I stuck in the logic of for loop,
load 'D:\MS\Research\Classification Model\Research Implementation\test.mat'; % loading test data i.e cp
[rI,cI] = size(cp);% size of test data
resultantImage = zeros(rI,cI); % image to store classified pixels
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:length(resultantImage)
classes = svmclassify(SVMModel,cp(rI(i),cI(i)),'Showplot', true);
if (classes == 'Y')
resultantImage(rI(i),cI(i)) = 1;
classes = 1;
else if (classes == 'N')
resultantImage(rI(i),cI(i)) = 0;
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
In the above for loop i want to extract the value from 'cp' variable and pass it to classifier for classification once i have pixel classified as 'Y' or 'N', i use the row col index of the pixel and store either 1 or 0 at the same row col index of resultant image. I have written the above logic but resultant image is not getting populated with 1 or 0 values it remains zeros array!!!!!!!!! Please help

Réponses (1)

Ingrid
Ingrid le 7 Avr 2015
This does not seem to be a problem with the for-loop. Are you sure that the SVMM model is correct and that it gives as output 'Y' and 'N' as possible groups? You can check for this by not using an if but a switch so in your case:
for i=1:length(resultantImage)
classes = svmclassify(SVMModel,cp(rI(i),cI(i)),'Showplot', true);
switch classes
case 'Y'
resultantImage(rI(i),cI(i)) = 1;
case 'N'
resultantImage(rI(i),cI(i)) = 0;
otherwise
error('Not classified in the correct class')
end
end
I always use the switch option when working with string variables as it makes the code more clear to read even if there are no problems in the implementation
  4 commentaires
Ingrid
Ingrid le 8 Avr 2015
you do not need to use the for-loop, vectorizing your solution would be much faster. This would look something like this
resultantImage = zeros(size(cp));
allClasses = svmclassify(SVMModel, cp, 'Showplot',true);
idx = (strcmpi(allClasses,'Y'));
resultantImage(idx) = 1;
rizwan
rizwan le 11 Avr 2015
Thanks,
But when i did this it prompts
The number of columns in TEST and training data must be equal.
Please help

Cette question est clôturée.

Community Treasure Hunt

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

Start Hunting!

Translated by