Attempt to access index out of bounds error in matlab - matlab

I am working on this code but I cannot figure out where I am going wrong
This is the part of the code that shows errors:
sf_num=0; sf_den=0;a=0; o=0;
for i=1:512
for j=1:512
sf_num=sf_num+(w1(i,j)*o(i,j));
a=a+(o(i,j)*o(i,j));
b=b+(w1(i,j)*w1(i,j));
sf_den = sqrt(sf_den + a*double(b));
end
end
and this is the error:
Attempted to access o(1,2); index out of bounds because numel(o)=1.
Error in ==> dwtcode at 44
sf_num=sf_num+(w1(i,j)*o(i,j));

You define o as: o=0 making it a scalar, meaning it only has 1 element.
You can't access index 1,2 of o because it doesn't have that many elements

Related

Error in loop: Attempted to access L(1); index out of bounds because numel(L)=0

I tried searching, but don't seem to see any solutions for my problem. It says
Attempted to access L(1); index out of bounds because
numel(L)=0.
Error in Energy_Management_code (line 64)
a(k)=L(1);
and this is the code that got error
%Arranging the data according to its location
L=NUMERIC(:,Location_no_column);
k=1;
a(k)=L(1);
p(1).loc=find(L/a(k)==1);
L(p(1).loc)=nan;
while max(isfinite(L))==1
next=min(FIND(isfinite(L)));
a(length(a)+1)=L(next);
p(length(a)).loc=find(L/a(length(a))==1);
L(p(length(a)).loc)=nan;
end
for i=1:length(a)
Location(i).ID=a(i);
Location(i).Place=p(i).loc;
Location(i).Number=length(Location(i).Place);
end
So from what I can see, the error is very simple.
a(k) = L(1);
This line errors out because numel(L)=0, which means L is empty. I'm guessing the line creates an empty L.
L = NUMERIC(:,Location_no_column);
Check what NUMERIC(:,Location_no_column) prints out and confirm.

Nested functions in matlab

I have a method that used is inside method, each one is working well independently but when one used inside one an error occurs
function [cov]=CC(a,b)
meana=mean(a);
meanb=mean(b);
entity=0;
for k=1:10
entity=entity+((a(1,k)-meana) * (b(1,k)-meanb));
end
cov=entity;
works fine but if used inside this method some error occur that says
??? Attempted to access b(1,10); index out of bounds because size(b)=[1,9].
Error in ==> CC at 9
entity=entity+((a(1,k)-meana) * (b(1,k)-meanb));
Error in ==> CM at 8
e=CC(dim(1,i:10),dim(1,j:10));
The function CM is shown below
function [covM]=CM(a,b)
dim=[a b];
for i=1:2
for j=1:2
e=CC(dim(1,i:10),dim(1,j:10));
end
end
covM=e;
So what does this statement " ??? Attempted to access b(10); index out of bounds because numel(b)=9 "
mean? b is [1,10] not [1,9]
The original variable 'b' is 1x10, but in the line:
e=CC(dim(1,i:10),dim(1,j:10));
you actually call the function CC, that gets two variables, and treat them again as a, b, but now they are different :
dim(1,i:10) now (inside CC) called a
dim(1,j:10) now (inside CC) called b.
Because of the loop, when j=2, then dim(1,j:10) is 1x9, and so variable b that inside CC function. this is the reason for the error.

matlab 'for' loop not executing last step or single step

I don't understand why the for cycle does not execute the last cycle, i.e. the field 'PX_TO_BOOK_RATIO'.
javaaddpath('C:\DocumentsandSettings\cascari\Desktop\API\APIv3\JavaAPI\v3.7.1.1\lib\blpapi3.jar')
connection = blp;
FileName='ScopeEconomies';
list={'OPM LN Equity';'FCTY US Equity';'FCCY US Equity'}
bbgfields={'TOT_LOAN_TO_TOT_ASSET'; 'PX_TO_BOOK_RATIO' };
nfields=length(bbgfields);
for i=1:nfields
[data,sec] = history(connection, list, bbgfields(i), '01/01/1993', '12/31/2013',...
{'quarterly','all_calendar_days','nil_value'},'USD');
temp=[data{:,:}];
eval([char(bbgfields(i)) '=temp']);
name=char(bbgfields(i));
xlswrite(FileName, temp, name, 'B1');
end;
When instead I leave only one element in bbgfields, I get
Attempted to access j(1); index out of bounds because numel(j)=0.
Error in blp/history>eventHandler (line 417)
outInd(i) = j(1);
Error in blp/history (line 245)
[d,sec] = eventHandler(b,s,f);
I am guessing it is because you are using a cell as an input into history. Have you tried converting it to char as follows?
[data,sec] = history(connection, list, char(bbgfields(i)), '01/01/1993', '12/31/2013',...
{'quarterly','all_calendar_days','nil_value'},'USD');

'Undefined function or variable' in Matlab

Here is my code:
function [im,sindx,end1]=alln(im,i,j,secret,sindx,end1)
slen=length(secret);
p=im(i,j);
neigh= [im(i-1,j) im(i+1,j) im(i,j-1) im(i,j+1) im(i-1,j-1) im(i+1,j-1) im(i-1,j+1) im(i+1,j+1)];
minpix = min (neigh)
maxpix = max (neigh)
if minpix < p < maxpix
lowlim = minpix+1;
highlim = maxpix-1;
range = highlim-lowlim+1;
nbits=floor(log2(abs(range)));
if sindx+nbits-1>slen
end1=1;
return
end
for k=1:nbits
bin(k)=secret(sindx+k-1);
end
b = bin2dec(bin);
newvalue1 = abs (minpix + b);
newvalue2 = abs (maxpix - b);
if abs(p-newvalue1)<= abs(p-newvalue2)
im(i,j) = newvalue1;
else
im(i,j) = newvalue2;
end
sindx=sindx+nbits;
end
end
My main program calls this function. When I run the program, I get the following error message:
??? Undefined function or variable "bin".
Error in ==> alln at 34
b = bin2dec(bin);
I know there are many experts for whom this is not a problem at all. I am new to MATLAB. Please guys, show me the way, which type of modification in the code can overcome this problem?
First of all, are there some lines missing from the file? Perhaps you've stripped some comments from the top? Because the error message says that
b = bin2dec(bin);
is line 34, but it's line 22 in the code you present.
OK, that aside...
The error message says that 'bin' isn't defined, but I see that it's being set on the line...
bin(k)=secret(sindx+k-1);
That suggests to me that THAT line isn't being run.
I see that that bin = ... line is inside of a 'for' loop, so I suspect that the for loop is run zero times, meaning that 'bin' never gets defined. What is nbits? Is it 1, or perhaps less than 1? THAT would prevent the loop from running at all.
Try removing the semicolon from the end of the
nbits=floor(log2(abs(range)));
line and run your code again.
Leaving off the semicolon will force the value of nbits to be printed in the Command Window. I bet you'll find that it's 1 or less. If that's the case, then start looking at HOW nbits is calculated, and I bet you'll find the problem.
At what input arguments to the function alln, are you getting the error?
Lets suppose that nbits is 0, then the following loop will not run:
for k=1:nbits
bin(k)=secret(sindx+k-1);
end
So, bin will be undefined. So, the error happens. This is one of the cases where the error can happen. There are many such possible cases.

going from command window (as script) to function

the program is working perfectly but when i am changing it to function the follwing error is beeing displayed:
[Parent1index, Parent1Position, alldcel] = Parent1n(TotalnoOfGrids, noOfNodes, Penalties, test)
??? Index exceeds matrix dimensions.
Error in ==> Parent1n at 10
[~,index]=min(alldcel{t});
Because alldcell{t} may not exist for some values of t if the condition to assign values to it in
if Penalties{t}(r)== 0;
alldcel{t}(r)=inf;
end
is never satisfied. Assume for some t that all values of Penalties{t} are different than zero. Then you would never assign inf to alldcell{t}. That means, you are only extending the cell array alldcell when Penalties{t} is zero for some r. If the condition is never satisfied, alldcell{t} will not exist and asking for it will give you a cell array error.
You should at least initialize it using alldcell = cell(TotalnoOfGrids,1).
Also, comparing for equality to zero using a==0 is not a good idea. You should use abs(a)<tol for some small value tol.
ok with this code the functiion worked:if Penalties{t}(r)> 0;
alldcel {t}(r)=alldcel{t}(r);
else alldcel {t}(r)=inf; but interchanging if statement with else did not