Is colon notation (:) equal to array(vector) [closed] - matlab

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have wrote a small program to test some funnction.
This is the proram:
close all;
clear all;
f = #(x, n) power(-1,(n-1)./2) .* power(x, n) ./ factorial(n);
n = [0,3,5,10,50,100];
% n = 0:10:100;
x = linspace(0, 4*pi, 1000);
ax = axes('nextplot', 'add');
for k = 1:length(n)
plot(ax, x, f(x, n(k)), 'displayname', ['f_', int2str(n(k)), '(x)']);
end
The main problem for me is that I thought that colon notation(1:10) definies array with values equaliy spread.
In my program that is not the case.
There is difference in output of the program when I set n as [0,3,5,10,50,100] and if I set n as 0:10:100.
In the first version, with array, the program works fine, but wit the second version, with colon notation, the program does not work it simply draws a line at 0.
So my question is way this is happening? I mean if the colon notation and the array definition are the same why does the program has different output for the colon notation and array notation?
Did I missunderstod something?
Thanks!!
EDIT:
This are the plots that I get:
First id array notation, second is colon notation.
I am using mathlab R2013a

In your commented line,
% n=[0:10:100];
you create a vector with values from 0 to 100, with spacing 10, ie [0 10 20 30 40 50 60 70 80 90 100].
With your uncommented line,
n=[0 3 5 10 50 100];
you have the values you specify.
Since they're not the same input, you won't get identical output.

Related

MATLAB: How to sort .txt data file with 1000 data points (10 collected each day for 100 days)? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I loaded in the 1000x1 .txt file but I need to make it 100x10? How do I do this in matlab?
Thanks
Reshape should do this for you. https://www.mathworks.com/help/matlab/ref/reshape.html
But you haven't explained any criteria regarding what the change of dimensions is based on. So, it may not be exactly what you want.
You can use the reshape function to do this. In the example below I created a column vector x that is 1000 x 1 containing numbers that ramp from 1 to 1000. I didn't know what order you wanted the rows and columns populated, so I created variables x2 and x3 with the two variations, you can choose the form that fits your needs.
x = (1:1000)';
% x2 is created with one column at a time
x2 = reshape(x, 100, 10);
% x3 is created one row at a time
x3 = reshape(x, 10, 100)';

Matlab incorrect computation result [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I got a few Matlab code to play. But the answer is not correct:
x = linspace(-pi, pi, 10)
sinc = #(x) sin(x) ./ x
sinc(x) // wrong result occurs at here.
The expected result as below:
ans =
Columns 1 through 6:
3.8982e-17 2.6306e-01 5.6425e-01 8.2699e-01 9.7982e-01 9.7982e-01
Columns 7 through 10:
8.2699e-01 5.6425e-01 2.6306e-01 3.8982e-17
real result:
ans =
Columns 1 through 3
0.000000000000000 0.263064408273866 0.564253278793615
Columns 4 through 6
0.826993343132688 0.979815536051016 0.979815536051016
Columns 7 through 9
0.826993343132688 0.564253278793615 0.263064408273866
Column 10
0.000000000000000
details: My OS is arch linux,
Matlab is downloaded through official website.
matlab version is 2015b
The expected result and the real results you present are identical as far as I can see.
The only difference is the notation: normal vs scientific.
With format short you can switch to scientific notation and get identical results with identical formatting.

why repmat(1,(1,10)) does not work in matlab? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Here is the code.
>> a=ones(1,10);
>> b=size(a);
>> repmat(1,b)
ans =
1 1 1 1 1 1 1 1 1 1
>> repmat(1,(1,10))
repmat(1,(1,10))
|
Error: Expression or statement is incorrect--possibly unbalanced (, {, or [.
Does anyone know why? and why does the error go like that? Thanks.
The expression size(a) returns [1 10], not (1,10). So the equivalent is:
repmat(1, [1 10]);
Here's some helpful documentation:
Matrices and Arrays
The size function
The repmat function
The full equivalent to your example code is repmat(1,size(ones(1,10))). Alternatively you can use a repmat(1,[1,10]), for array construction you have to use [], the round brackets () are for function call and indexing only.
Repmat is used to create an array with n repeating copies of the source array (A). Hence, the second argument is a single scalar defining the number of copies of A, the first argument.
You can use a vector for the second argument, but it defines how many copies the result has in different dimensions. Syntax would be: repmat(1,[1 10])

How do I combine two vectors of different size in a matrix? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm new to MATLAB and I'm having trouble with what might be a basic concept. I would really appreciate any insight and help.
I have to define the function z=10*x*sin(⁡2*y) over the range of 0 ≤ x ≤ −1, 0 ≤ y ≤ 3. I have been specifically asked to have a vector consisting of 11, equally spaced values of 0 ≤ x ≤ −1 and another vector consisting of 31 equally spaced values of 0 ≤ y ≤ 3. Then I have to define a 31×11 matrix z, with entries z(m,k)=10*x(k)*sin(2*y(m)).
I have no idea what m and k have to do with anything. And anytime I try to insert x and y into z, I get an error message about inner dimensions.
All I have is:
x=-1:.1:0
y=0:.1:3
Hint: see the ndgrid() command.
x=-1:.1:0;
y=0:.1:3;
[X,Y]=ndgrid(x,y);
Z=10*X.*sin(2*Y)
Don't forget the dot in ".*" when multiplying matrices element by element
May this is the solution?! Have a look at the element-wise operator ".*"
x = -1:.1:0;
y = 0:.1:3;
[X,Y] = ndgrid(x,y);
z = 10*X.*sin(2*Y);
mesh(z);
Perhaps this will help you! if i understood correctly:
x=-1:0.1:0;
y=0:0.1:3;
take_size_x=size(x,2);
take_size_y=size(y,2);
for j=1:take_size_y
for i=1:take_size_x
z(j,i)=10*x(i)*sin(2*y(j));
end
end

Value from intrerval [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
there is a C variable, which has 300 samples and there is a range on X axis, on which the variable is spread. I need to sort the C samples and establish, to what interval it belongs to. The X range is divided to 100 intervals (%). So I need to create a script, which will take interval between int(i) and int(i+1), proceses whole C and save the suitable C(i) into a variable D and make an average from it. Thanks a lot. Tom
You can do this with a combination of histc and accumarray. First let's generate some data -
>> X = rand(3000, 1); // 3000 samples of the variable X
>> C = X + randn(3000, 1); // 3000 samples of C, which depends on X
>> edges = linspace(0, 1, 101); // edges of the bins for X
Now you can find out which bin each observation falls into using the second output of histc
>> [tmp, bin] = histc(X, edges);
Finally, you can create a vector Cavg using accumarray to iterate over each bin and take their average
>> Cavg = accumarray(bin, C, [101,1], #mean, NaN);
You can plot the observations and their average to check that you got what you expected
>> plot(X, C, '.');
>> hold on;
>> plot(edges, Cavg, 'r', 'LineWidth', 2);