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.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
In a sample code I have X(1:n, 1)
I don't understand how it works
n=3 in a sample
Is it a matrix? but isn't () used for indexing so how it's a double index?
I imagine that your X is a two-dimmensional matrix. Generalizing, it can be said that to access the elements of a matrix is done with: X(n,m).
The common case is to get one single number, in that case n and m are integer numbers. But you can also pass vectors to n and m positions and that way extract a submatrix from the original one.
As an example:
X = [1 2 3; 4 5 6; 7 8 9]
X(1:3,1) = [1; 4; 7]
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 6 years ago.
Improve this question
I have a matrix 200 *10 having values between 100 and 150 and I want to have values of between [0 1] that's it.
I know one way of doing is to divide it by max value but I want to know any other way i.e built in function of matlab or any other mathematical formula which shifts all values to less than one. Kindly do not mention sigmoid function as I have tried.
Thanks
To normalize a matrix X such that values lies in the range [0, 1], you can use:
Xnorm = (X - min(X(:)))/(max(X(:)) - min(X(:)))
Dividing by a number is much more computationally expensive than a comparison. Try something like
m = max(A(:))
B = A(A<m)
This is called "logical indexing". The result will be a vector of values in A smaller than the maximum m. Of m doesn't have to be the maximum though... you could equally well substitute any value of m and logical indexing will work.
For instance, in the above code, if
A =
[0 3 4
2 0 4
5 5 2
4 5 3
5 4 1 ]
B.' = [ 0 2 4 3 0 4 4 4 2 3 1 ]
Let me know if this is what you had in mind. It was a bit unclear from the question.
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 need to generate the trial order of my stimuli for an experiment consisting of 10 experimental blocks:
There should be 100 trials per block.
There are 20 images as stimuli.
Within each block, the 20 stimuli should
be shown 5 times each.
The order of the stimuli should be fully randomized.
(Meaning, NOT 1:20 in random order, then 1:20 in random order, and so on.
All 100 trials should be randomized across each block!)
I have to make a matrix that represents the trial order of my experiment, in which the rows represent the 10 blocks, and the columns represent the stimuli to
be shown in order from column 1 - to column 100.
I figured out that I should use the function repmat(), but I can't solve this.
This will do it, just adjust your values for the number of blocks and block size according to your needs. No repmat used though.
Nblocks = 10;
Nchoices = 20;
Ndisp = 5;
Ntrials = Ndisp*Nchoices;
array = ceil([Nchoices/Ntrials:Nchoices/Ntrials:Nchoices]);
perms = array(cell2mat(cellfun('randperm',mat2cell(Ntrials*ones(Nblocks,1),ones(Nblocks,1),1),'UniformOutput',0)));
It's a good idea to split the longer wrapped command into individual steps if you want to make sense of it in more depth. Look in particular at the documentation for individual functions and particularly ceil and randperm.
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 have the following data: ET = [1 3 5 7 6 4], and below is my code:
for i=1:3
meanET(i)=ET(:,1+(2*i-2)); %//for i=1,extract ET column 1 data
stdET(i)=ET(:,2+(2*i-2));
totalET(i)=meanET(i)+stdET(i)
end
However, MATLAB display's an error that says that in the assignment A(I)=B, the number of elements in B and I must be the same, and therefore I modified my code to this:
for i=1:3
meanET=ET(:,1+(2*i-2));%for i=1,extract ET column 1 data
stdET=ET(:,2+(2*i-2));
totalET=meanET+stdET
end
After running the latter code, it showed meanET=6, stdET=4, and totalET=10, which means that it only stored the data for i=3 in the workspace. I want to get the result like
totalET=[4 12 10] in the workspace, corresponding to i = 1, 2, 3. How do I do that?
OR you could just go with a simple vectorized solution:
>> totalET = ET(1:2:5) + ET(2:2:6)
totalET =
4 12 10
you should just declare your target array at the beginning of your code:
meanET=zeros(size(ET,1),3);
stdET=zeros(size(ET,1),3);
for i=1:3
meanET(:,i)=ET(:,1+(2*i-2));
stdET(:,i)=ET(:,2+(2*i-2));
end
totalET=meanET+stdET
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I used ARX function then RESID function from the System Identification Toolbox, but the resulting residuals are:
0
0
0
5
6
8
7
8
the number of zeros=the number of lags, I need a complete vector of residuals
An AR model of order N needs the previous N values to predict the next one, which is why the first N are not predicted. You can always pad the vector at the beginning (either by replication or zeros), example:
load twotankdata
order = 5;
m = arx(y, order);
r = resid([y(1:order);y], m);
r = r(order+1:end);