The size function does not return the right number
Show older comments
%trim the calibrated data
m = unique([in_M, code_M],'sorted', 'rows');
[len, notImp] = size(m);
for j = 1:1:len
if m(j,2) > 4095
m(j,:) = [];
else
continue;
end
end

The size of m is 999961 x 2, but the returned value of the number of rows(len) is 1000001
Could you help me with this matter? Thank you in advance!
4 Comments
After measuring its size your code removes rows of m:
if m(j,2) > 4095
m(j,:) = [];
..
After that code has run, it is entirely expected that m will have <=1000001 rows. Which apparently it does.
So far everything is behaving exactly as expected, it is unclear what the problem is.
Xin
on 10 Nov 2022
Stephen23
on 10 Nov 2022
The problem is that inside the loop you are removing rows and then trying to access rows which no longer exist.
Consider this vector: [1,2,3,4]
we start running a loop over it and remove the 2nd element, so the vector now look like this: [1,3,4]
then the loop keeps running and then we try to remove the 4th element. But does the vector have a 4th element? (hint: no)
Thus the error: you are trying to remove rows which do not exist.
The usual solution is to loop over the rows from bottom to top:
for j = len:-1:1
Xin
on 10 Nov 2022
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!