Value from intrerval [closed] - matlab

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);

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)';

Cascading waveforms onto a single variable in matlab [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 7 years ago.
Improve this question
I have 3 signals:
a = ecg(1000); // clean ecg with no noise
b = a + noise1; // with noise component on the ECG
c = a + noise2; // a,b,and c have the same dimension (1000x1)
now, i want to put the concatenated signal onto a single variable, x, so that the output of x would be:
x = a concatenated with b; b concatenated with c;
If by cascading you mean concatenating, simply do this:
x = [a b c];
This simply pieces a, b and c together so that they are one signal and the three signals are side-by-side each other. Doing ecg(1000) creates a 1 x 1000 vector. Assuming your noisy signals after that respect the same dimensions, then the above syntax should hold.

Is colon notation (:) equal to array(vector) [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 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.

Multiple draws from multivariate normal distribution in Matlab [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Let
MU=[1 2; 3 4; 5 6]
SIGMA=[2 0; 0 2]
I want to write some lines of code in Matlab to draw R=10 unobservables from Normal((MU(1,:),SIGMA), Normal((MU(2,:),SIGMA), Normal((MU(3,:),SIGMA) without looping and store the results in a matrix
3x(R*2).
Firstly from the sigma, you can see it is independent normal variables. So you don't need to use mvnrnd function. Just use randn which creates variables with zero mean and standard deviation of 1.
numSamples = 10;
mu = [1 2 3 4 5 6];
sigma = 2;
samples = sigma.*randn(numSamples, 6);
samples = bsxfun(#plus,samples,mu);
Used this because you said no looping.

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