Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

For loop Issue, Loops, Row Col Indexing Issue

3 Ansichten (letzte 30 Tage)
rizwan
rizwan am 7 Apr. 2015
Geschlossen: MATLAB Answer Bot am 20 Aug. 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

Antworten (1)

Ingrid
Ingrid am 7 Apr. 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 Kommentare
Ingrid
Ingrid am 8 Apr. 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 am 11 Apr. 2015
Thanks,
But when i did this it prompts
The number of columns in TEST and training data must be equal.
Please help

Diese Frage ist geschlossen.

Community Treasure Hunt

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

Start Hunting!

Translated by