Passing array to a function

조회 수: 11 (최근 30일)
Tom
Tom 2011년 3월 8일
Hi,
I'm getting an error message when attempting to pass some variables (including arrays), and can't figure out why.
Here is my code..
frequency(x) = getFreq(ts, Rdata, Ldata, foot);
Ldata is a 819x3 double, Rdata is a 1362x3 double, foot=1, ts=10959
On the function side, the parameters are
function Freq = getFreq(timestamp, SessionL, SessionR, foot)
The error message I'm getting is
??? In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in ==> main at 71 frequency(x) = getFreq(ts, Rdata, Ldata, foot);
Thanks

답변 (3개)

Teja Muppirala
Teja Muppirala 2011년 3월 8일
Whatever Freq is when you exit the function getFreq, it is not of a size compatible with the index vector x.
For example, this will also give you the same error.
A = [0 0 0];
x = [1 3];
A(x) = sum( rand(3) )
This fails because x only has two values, but the sum returns a vector of 3 values. This, however, is OK:
A = [0 0 0];
x = [1 3];
A(x) = sum( [1 2; 3 4] )
So just go make sure that the value getting returned (Freq) is of a proper size to match with x.
Try the following:
temp = getFreq(ts, Rdata, Ldata, foot);
size(temp)
size(x)
and compare what you get. That should help you see why it's not working.

Brett Shoelson
Brett Shoelson 2011년 3월 8일
You'll need to show us a bit more. It would be particularly useful, for instance, to see line 71 of getFreq.
Consider putting a breakpoint there and re-running. Are the variables what you expect when you get to that point? If so, you have a logical flaw in your code. If not, trace backward and figure out how/why something got changed.
Brett
  댓글 수: 1
Tom
Tom 2011년 3월 8일
sorry i should have stated, line 71 is the first line shown, the line calling the function. The variables are how i expected, I just cant understand why that type of error would occur

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


Matt Tearle
Matt Tearle 2011년 3월 8일
What Teja said.
I hate to ask the dumb question, but do you actually want to do indexing on line 71? Many people write things like foo(x) = ... because they're used to thinking in mathematical terms (foo is a function of x). But [voice = Morbo] "MATLAB does not work that way" [/voice]. In MATLAB, foo(x) = ... means "calculate the RHS (...) and put the result into the elements of foo indexed by x". So the fundamental dumb question to ask is: are you really trying to save the output from getFreq into a subset of the variable frequency, in which case do what Teja said to check dimensions, or are you actually just trying to save the output of getFreq to a variable called frequency, in which case get rid of the (x).
  댓글 수: 1
Matt Tearle
Matt Tearle 2011년 3월 8일
Er, just to clarify: "dumb" question not because *you're* being dumb, but because *I* feel dumb asking, when it's entirely possible that, yes, you do want to do that.

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by