Dear all
I just began to work w Matlab. I have an array with decimal numbers and zeros from which I have to calculate log10 and write a new array, but it doesn't work. When i run this i got error "Attempted to access log10(0.946491); index must be a positive integer or logical." code is as follows
log10=zeros(2030,1354);
for j=1:1354
for i=1:2030
log10(i,j)=R547(i,j)/R531(i,j);
end
end
log10(isnan(log10)) = 0 ; %my array
>> for j=1:1354
for i=1:2030
if log10(i,j)~=0
X(i,j)=log10(double(log10(i,j)));
end
end
end
Anyone please help me
Thank you

댓글 수: 1

Stephen23
Stephen23 2014년 12월 19일
편집: Stephen23 2014년 12월 19일
This is the danger of overwriting inbuilt function names with your own functions or variables. Some common ones that should be avoided:
  • log
  • i, j
  • plot
  • sum
  • input
  • size, length, etc.

댓글을 달려면 로그인하십시오.

 채택된 답변

Thorsten
Thorsten 2014년 12월 18일

1 개 추천

You overwrite Matlabs log10 function with your log10 matrix; rename it and everything should work fine.

댓글 수: 5

Alina
Alina 2014년 12월 18일
편집: Alina 2014년 12월 18일
I tried but the answer is the same
U=zeros(2030,1354);
for j=1:1354
for i=1:2030
U(i,j)=R547(i,j)/R531(i,j);
end
end
U(isnan(U)) = 0 ;
>> for j=1:1354
for i=1:2030
if U(i,j)~=0
X(i,j)=log10(double(U(i,j)));
end
end
end
Attempted to access log10(0.946491); index must be a positive integer or logical.
Thorsten
Thorsten 2014년 12월 18일
편집: Thorsten 2014년 12월 18일
Your log10 matrix is still in the workspace; use
clear log10
And then have a look which log10 is used now
which log10
BTW: you can get rid of the for loops:
U = R547./R341;
U(isnan(U)) = 0;
idx = find(U~=0);
X(idx) = log10(U(idx));
Alina
Alina 2014년 12월 18일
Thank you Thorsten
It works, but some answeres are 0.000000000000000 + 0.000000000000000i where must be 0
Thorsten
Thorsten 2014년 12월 18일
Use
X = real(X);
Alina
Alina 2014년 12월 18일
Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2014년 12월 18일

1 개 추천

Try this
clear log10
U=zeros(2030,1354);
for j=1:1354
for i=1:2030
U(i,j)=R547(i,j)/R531(i,j);
end
end
U(isnan(U)) = 0 ;
for j=1:1354
for i=1:2030
if U(i,j)~=0
X(i,j)=log10(double(U(i,j)));
end
end
end
Star Strider
Star Strider 2014년 12월 18일

1 개 추천

Your code can be reduced to this:
R547 = randi(50,10,15); % Created Data
R531 = randi(50,10,15); % Created Data
Log_10 = double(R547./R531); % Do Element-Wise Division
Log_10(1:5:end,1:5:end) = NaN; % Created Data (Insert NaN)
X = log10(Log_10); % Take Logs
X(isnan(X)) = 0; % Set ‘NaN’ Values To Zero
First, please never name your variables the same as built-in MATLAB functions, such as log10. MATLAB will take the most recent definition (as it did in your code) and treat it as a variable rather than a function. I renamed your ‘log10’ array ‘Log_10’ to be sure that you do not ‘overshadow’ the log10 function.
Second, if you need to do element-wise operations, use the dot operators, for instance instead of (/), use (./), (*), use (.*) and (^), use (.^).
Third, the log10 of NaN is NaN, so you only need to test for it and replace it after you do everything else.

댓글 수: 2

Alina
Alina 2014년 12월 18일
Thank you. Now I know it.
Star Strider
Star Strider 2014년 12월 18일
My pleasure!

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Historical Contests에 대해 자세히 알아보기

제품

질문:

2014년 12월 18일

편집:

2014년 12월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by