필터 지우기
필터 지우기

Problems with multiplications (matrix dimensions must agree) on F5, but not on F9

조회 수: 1 (최근 30일)
I have this code:
function [Pex] = ponderacaoPorEspecies(probabilidadeDeExtincao)
% Este módulo pondera a probabilidade de extinção das espécies em
% função das forças de interação entre espécies. A probabilidade
% varia em função de uma proporção dela mesma.
global forcaDeInteracoesEntreEspecies probabilidadeDeExtincao Especies
Pex = probabilidadeDeExtincao .* (1 - forcaDeInteracoesEntreEspecies);
It is used by another code, where the variables are given. After I F5 this main code, I get:
"_??? Error using ==> times Matrix dimensions must agree.
Error in ==> ponderacaoPorEspecies at 7 Pex = probabilidadeDeExtincao .* (1 - forcaDeInteracoesEntreEspecies);
Error in ==> compilador at 68 probabilidadeDeExtincao = ponderacaoPorEspecies(probabilidadeDeExtincao)_"
However, if I get to the fuction code above, select the line:
Pex = probabilidadeDeExtincao .* (1 - forcaDeInteracoesEntreEspecies);
And press F9, the results shows normally. Whats is going on?

채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 12월 30일
Your variable 'probabilidadeDeExtincao' is an input argument to the function, while it is also a global variable. This could be the problem.
In any case, it should be relatively easy to debug this problem. Set a break point in your main code, press F5 and then F10 to step through the code. Press F11 to step into the function. When it reaches this line, go to Command Window, check the size of the variable 'probabilidadeDeExtincao' and 'forcaDeInteracoesEntreEspecies', you'll find the problem.
size(forcaDeInteracoesEntreEspecies)
  댓글 수: 2
Igor de Britto
Igor de Britto 2011년 12월 30일
Thanks for the input, Fangjun. When I substituted the forcaDeInteracoesEntreEspecies for ~ on the function, it did work just fine. What puzzles me is that I have 3 other functions where variables are defined both as argument and global and they all seem to work fairly ok. Odd.
Fangjun Jiang
Fangjun Jiang 2011년 12월 30일
Well, I don't think it's odd. The variable should not be an input argument and at the same time be a global variable. It will cause conflict and you are at the mercy of whether the value or size of the variable matches.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by