Sampling with vectors at the same time

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 个评论

Can you explain what do you want to do?
What does that mean?
How can I make understand first vector is a sampling and second is a vector of values?
with sampling I mean repetition of the experiment with different d (scalar) values, that will be returned in this way:
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
for each experiment used for plotting out how evolves in this 20 exp the function "sample".
A vector is an array.
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)).
d is a scalar, i want to test my function with 20 different d values evolving from 1 to 20 so:
SUM (cos(2*pi*1*A) )
SUM (cos(2*pi*2*A) )
SUM (cos(2*pi*3*A) )
.....
so in the end i'll receive
Columns 1 through 11
a b c ..
Columns 12 through 20
...
scalar results of a sumatory of cosinus of de values in vector
PEXAMPLE: cos(33,3)+cos(45,3)+....(the sum as long as the lenght of the vector A). so should return an scalar.
d will be 1 -> cos(33,3)+cos(45,3)
then
d will be 2 -> cos(66,6)+cos(90,6)
then ...
See the code in my comment.
yes but A is NOT of twenty values, in my code A is 8128 and i just want 100 experiments anyway.. No more resolution needed, it has nothing to do because d is a scalar. in the end d will be a 1 or a 2 or a 3, it will be performed in 20 experiments or (100 in my code) (as 20 inputs are passed) and then returned this way:
Columns 1 through 11
a b c ..
Columns 12 through 20
...

请先登录,再进行评论。

 采纳的回答

Try this:
d = 1:20;
A = [24,34,56,66,86];
plot(d,arrayfun(@(x)sum(cos(2*pi*A*x)),d))
Obviously, in this simple case, the answer will always be 5 for all d since we are dealing with integer multiples of 2*pi (and there are only 5 elements in A), but I think this is what you are looking for.

2 个评论

That was PRO!! That's what i'm searching for, Thank you!!
I'll search with the arrayfunc to draw my function, one thousand thanks to all.
To close the topic, you have to accept the answer which helped you

请先登录,再进行评论。

更多回答(4 个)

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 个评论

Yes, it's sum(...). A is a vector of random values that will be added themelves, nothing to do with the number of experiments d i want to test out of the function. In reallity, but youre right, that's what i said.
I've changed SUM to sum and (2*pi*d.*A) and still the same problem.
Mybe i don't know how to plot, i want to plot the function with different d's how you would do it?
Thank you
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?
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.
No, d is allways a scalar i want to test 20 times first d will be 1 then d will be 2 then d will be 3, so the cos argument will groth x1 x2 x3 and i'll need to plot how the funtion evolves with different arguments.
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)?
Hi again, with your code in a new script file, it prompted:
??? Error: File: prueba.m Line: 8 Column: 1
Function definitions are not permitted in this context.
in the LINE: function theSum = sample(d)
I've created an script file with your code since the for is supposed to be inside a function. How do yo do to have 2 functions without calling a for at the main. This tool is expected to be used in my project so it would be cool that it was inside a function.
Thank you.
It worked typing 60% of your code on the command window but i need something more elegant, Matlab is quite stranger that i though.
Need help to write some code equivalent to this almost all in a function
Very grateful anyway.
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.

请先登录,再进行评论。

Maybe you mean indexing
v=[2 3 5 4 10 20]
d=[2 3 1 4 5 6]
new_v=v(d)

1 个评论

I meant to perform multiple experiments due to an entry of different values the d only affects for each experiment the d is first 1, the sum is done, then the d is two, the sum is done, then the d is three....
and then the result is returned of the function with 20 different d's as 20 different inputs to the function, the matlab wants to understand is a vector to multiply and is only 20 experiments so D is an scalar.

请先登录,再进行评论。

Alfonso
Alfonso 2013-2-24
编辑:Alfonso 2013-2-24
<< d=[1:1:20]
<< sample(d)
no more code needed.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function sample(d)
A = [24,34,56,....]
SUM (cos(2*pi*d*A) )
end
Alfonso
Alfonso 2013-2-24
编辑:Alfonso 2013-2-24
MAYBE Im complicating myself, forget about the 20 (Or 100)entries, how would you plot:
function sample(d)
A = [24,34,56,....]
SUM (cos(2*pi*d*A) )
end
I used to plot
plot (d,sample(d))
I want function to evolve non like an integer (0,1:0,1:20) instead, so i want increases of the X axis of 0,1 from 1 to 20 HOW WOULD YOU PLOT IT? I simplified in your example, but i don't want a deffault plotting progress, so i use multiple entry (d=[0,1:0,1:20]).

类别

帮助中心File Exchange 中查找有关 Entering Commands 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by