Sampling with vectors at the same time
Show older comments
Hello,
its a simple question, but i havent managed to solve it:
I have to sample a vector and the matlab doesn't understandme and prompts:
??? Error using ==> mtimes
Inner matrix dimensions must agree.
I wanted to sample a SUM (cos(2*pi*d*A) )where A is a vector of many degrees value, so I have:
function sample(d)
A = [1,2,3,....]
SUM (cos(2*pi*d*A) )
end
and the input is:
d=[1:1:20]
How can I make understand first vector is a sampling and second is a vector of values? (The idea is plotting (sample,d) )
The fact is that the expresion of the vector is a real for each value of d so I should expect something returning results of the SUM of different d's like:
Columns 1 through 11
1 2 3 4 5 6 7 8 9 10 11
Columns 12 through 20
12 13 14 15 16 17 18 19 20
But it doesn't understand each
Thank you
7 Comments
Azzi Abdelmalek
on 24 Feb 2013
Can you explain what do you want to do?
Azzi Abdelmalek
on 24 Feb 2013
What does that mean?
How can I make understand first vector is a sampling and second is a vector of values?
Image Analyst
on 24 Feb 2013
d is not a scalar in your main program. d is an array. Perhaps you want to take a single value of d when you call sample, like sample(d(10)).
Image Analyst
on 24 Feb 2013
See the code in my comment.
Accepted Answer
More Answers (4)
Image Analyst
on 24 Feb 2013
Edited: Image Analyst
on 24 Feb 2013
What is SUM? Is it a custom function you wrote, or an array of yours, because it's not the built-in MATLAB sum() function because MATLAB is case sensitive.
Perhaps you meant something like this:
A = 1:20
d=[1:1:20]
cosA = cos(2*pi*d.*A)
theSum = sum(cosA)
Note the .* instead of * between d and A to do an element by element multiplication, not a matrix multiplication like you were trying to do.
8 Comments
Image Analyst
on 24 Feb 2013
That can't be, unless you also redefine pi as an array. Did you run my code? Because it works fine. Is your A perhaps a 2 or 3 dimensional array, unlike the 1D array that you showed?
Also, your function sample() does not return any value, so what good does it do?
Image Analyst
on 24 Feb 2013
Edited: Image Analyst
on 24 Feb 2013
dMain = [1:1:20]
for k = 1 : length(dMain)
% Call sample() once for each value of d
theSumMain(k) = sample(dMain(k));
end
theSumMain % Print to command window
function theSum = sample(d)
A = 1:20;
cosA = cos(2*pi*d.*A);
theSum = sum(cosA);
IMPORTANT NOTE: you're just sampling the cosine at multiples of 2*pi, so all the values will be 1 and all your sums will be 20.
Alfonso
on 24 Feb 2013
Image Analyst
on 24 Feb 2013
Okay....isn't that exactly what my code did? Did you see how I called the function 20 times with different scalar values of d, taken from the dMain array (the collection of all d's you want to use)?
Image Analyst
on 26 Feb 2013
Alfonso, you need to read the getting started guide. You can't just type all that code into a single m-file because you can't have a script followed by a function in the same file. The main program can be in one m-file, and the other function in another m-file, OR if you want them to be in the same file, then you must have a function name at the beginning of the file with the same name as the m-file. For example if it's test.m, then your code would be
function test()
dMain = [1:1:20]
for k = 1 : length(dMain)
% Call sample() once for each value of d
theSumMain(k) = sample(dMain(k));
end
theSumMain % Print to command window
function theSum = sample(d)
A = 1:20;
cosA = cos(2*pi*d.*A);
theSum = sum(cosA);
If it's like that they can all be in the same m-file. I guess I assumed that you knew this.
Azzi Abdelmalek
on 24 Feb 2013
Edited: Azzi Abdelmalek
on 24 Feb 2013
Maybe you mean indexing
v=[2 3 5 4 10 20]
d=[2 3 1 4 5 6]
new_v=v(d)
Categories
Find more on Entering Commands in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!