"Operands to the || and && operators must be convertible to logical scalar values" occurring with integer comparisons
5 views (last 30 days)
Floris van den Broek on 10 Apr 2017
I have written a piece of code that essentially has this structure:
function [output] = spectralSVD(S,thr)
% S: [mxn] complex double
% thr: real double, value between 0 and 100
E_tot = trace(abs(S));
n_sv_max = min(size(S));
n_sv = 0;
E_rec = 0;
while E_rec < thr && n_sv <= n_sv_max
n_sv = n_sv+1;
E_rec = 100*cumsum(diag(abs(S(1:n_sv,1:n_sv))))/E_tot;
At this point, the code crashes: "Operands to the and && operators must be convertible to logical scalar values."
So as far as I understood, the && would be appropriate here since both conditionals are single-value comparisons (none of E_rec, thr, n_sv and n_sv_max are vectors or matrices). However, I still get the error message mentioned in the title. I have tried converting all of the aforementioned variables with the uint8() function (even though that does not do exactly what I want), but to no avail. It would appear that I misunderstood the explanation I read in other questions on the same error message.
Could anyone explain me where my thought process is going wrong, and how to appropriately fix my code? It would be very much appreciated!
Guillaume on 10 Apr 2017
Edited: Guillaume on 10 Apr 2017
"both conditionals are single-value comparisons (none of E_rec, thr, n_sv and n_sv_max are vectors or matrices)"
The first time through the while loop, they are. The second time... not so much. See the output of:
with n_sv = 0 as you've declared.
The best way for you to solve this sort of problems is to use the debugger to check what the values of the variables actually are as opposed to what you think they are.
I suspect n_sv should start at 1.