Matlab: Rolling function in matlab - matlab

I am a borderline novice in Matlab.I am trying to write a rolling function of CMSE
(Compose Multiscale Entropy) over a time series. I tried slidefun but that only works when the output is a scalar and the output for CMSE is a vector. The rolling window for the time series is supposed for 500 and the ouput of each windowed CMSE is a 100 x 1 vector. XX is the time series.
roll_CMSE_100=zeros(100,(length(xx)-499));
for i=1:(length(xx)-499)
roll_CMSE_100(i)=CMSE(xx(i:(499+i)),100)
end
I get the following output
??? In an assignment A(I) = B, the number of elements in B and
I must be the same.
Thank you for your time and consideration

Matlab is telling you the problem: you are assigning to the element in position "X" a vector but should be a number, because roll_CMSE is a matrix. Or you use cell array or you make assignment correctly.
If the output of CMSE(xx(i:(499+i)),100) is a 100x1 vector the correct way to assign the values is
roll_CMSE_100=zeros(100,(length(xx)-499));
for i=1:(length(xx)-499)
roll_CMSE_100(:,i)=CMSE(xx(i:(499+i)),100)
end
This simply assigns the ouput to column "i" of roll_CMSE matrix.

Related

(MATLAB) On drawing elements randomly from vectors and rows randomly from matrices without replacement in for loops

So, two related questions for code I'm writing for an experiment I'm building in MATLAB. Firstly, I have a 20 row column vector, called block1 (with a number in each of the 20 slots).
I want to draw a number randomly from the vector without replacement and repeat this until there are no numbers left in the vector (so 20 times total). I have the following line within a for loop (which is set to run for 20 iterations), which draws the random number from the vector and sets it equal to variable rand_num:
rand_num = randsample(block1,1,false)
It draws the random number just fine, but the problem is that the draw without replacement part doesn't work. Initially I thought this was because the vector was reset at the beginning of each iteration of the for loop. To make sure, I debugged and found that even when I stop the for loop before the first iteration finishes (and after the random number has been drawn), there are still 20 numbers in the vector (when there should be 19). Any ideas as to how I can fix this?
The second question is as follows. For the same experiment, I have a 20 by 6 matrix called pick_matrix. During each iteration of the same for loop as above, I want to pick one row at random from the matrix (including every element in that row; so 6 elements total for each row) without replacement and assign that row to variable random_row.
I figured out the code to pick a row at random and assign it to random_row, but I'm having the same problem as with the last question: the without replacement part isn't working.
randsample works fine, and exactly as expected. You just need to call it once before your loop rather than twenty times within the loop. Read the help carefully - randsample does not change anything about the input variable block1. Generally speaking (ignoring globals and such), input variables are not changed by Matlab functions, even if they are changed within a function, unless they are then explicitly output.
e.g. consider this:
function y = myfunc(x);
x = x*2;
y = x*3;
disp(x)
end
Within the function, x is doubled. Say I have a variable x in my workspace, value 10. If I call y = myfunc(2), I will see "4" displaced - and y will be output as 12, not 6. However, the x in my base workspace is never changed, which is exactly the expected behaviour.
If I want x to be changed, I have to set it explicitly as output to the function and deliberately call the function in such a way as to overwrite my variable x, e.g [x y] = myfunc(2);
As suggested in the comments, you could use randperm instead if you're taking every number, but a more generic example using randsample to take a random pick of 10 from 20:
% sampling without replacement is default
rand_nums = randsample(block1, 10);
rand_ind = randperm(20,10); % 10 numbers from 1:20
for n = 1:10
rand_num = rand_nums(n);
rand_row = pick_matrix(rand_ind(n),:);
% whatever you need to do with these things goes here
end

MATLAB: Automatic assigning of matrix element indices

I am currently in the process of writing a custom function to compute the RREF of a given m x n matrix. Since I am a complete newbie to MATLAB, I thought it would be a good idea to sample the built-in rref() function.
While examining the part of code that found "the value and index of largest element in the remainder" of the leading column, I had that:
[p,k] = max(abs(A(i:m,j)))
where m is the number of rows of the matrix, and i=j=1.
I understand that max(abs(A(i:m,j))) gives you the value of the largest element in the leading column - a single scalar answer. However, I cannot understand why it manages to assign two values to [p,k], with kbeing the index number for p. could someone please be kind enough to help?
k is the position in your vector where the maximum value is.
For instance, assume we use the vector [1,2,5,2,1]. There the max value is 5. This value is at the third position in the vector. So [p,k] = max([1,2,5,2,1]);will return p=5 and k=3.
The function will assing values depending on how you call it.
p = max(...
will assign only p
[p,k] = max(...
will assign p and k.

Matlab: indexes and associated values

First of all, my programming skills are not that great, so please bear with me.
I'm trying to script the following in Matlab, R2015a:
1) Given a certain vector (mag), calculates two notable values from the first 300 values of that vector (Am and A);
2) Gets the indexes of the value of Am, and the two indexes of the value A (the vector is symmetric, so there will be two indexes for A);
3) Gets the values (fm, f1 and f2) associated with the three previous indexes (index from Am, first A and second A respectively) from another vector (freq).
4) Finally, calculate D based on the values in 3).
So far, this is what I've got:
Am=max(mag(1:300)); %Am is the maximum value of vector mag
A=Am/2^0.5; %A is the other desired value
[~,Im] = mag(1:300,Am); %Trying to get the Am index. Error: "Indexing cannot yield multiple results." I found that this error is usual when using variables with the same name, which is not the case.
fm=freq(Im); %Value of freq associated with Am
[~,I1] = mag(1:300,A,'first'); %Index of the first value of A
f1=freq(I1) ; %Value of freq associated with the first value of A
[~,I2] = mag(1:300,A,'second'); %Index of the second value of A
f2=freq(I1); %Value of freq associated with the second value of A
D=(f2^2-f1^2)/(4*fm)
I'm having trouble getting the associated freq values from the desires values of mag.Any tips and suggestions are more than welcome.
And thank you in advance!
Here some suggestions:
%find the index of a certain value
l1=find(mag(1:300)==Am);
% find the first index
l2=find(mag(1:300)==Am, 'first');
usually, if you have a vector with indices, then you can use that to get the value of another vector, like
a=[1 3 6 8];
b=rand(10,1);
b(a) % yields the first, third, sixth and eighth value of b
Keep in mind, the index-vector needs to be integer type, else it won't work.
Can you maybe post the precise error message and maybe also an example of mag?
hope this helps

Matlab interpolation to switch dependent variable

I have an Nx2 matrix with columns as 'Time' and 'Progress'.
Progress is integral and Time is a real value corresponding to each progress unit.
I want to reverse the dependency and make 'Time' integral and output the fractional 'Progress' at every unit time step.
How can this be done?
Use interp1(Progress,Time,TimesWanted) where TimesWanted is a new vector with the times that you want. For example:
Progress=1:10; %just a guess of the sort of progress you might have
Time=Progress*5.5; %the resulting times (say 5.5s per step)
TimesWanted=10:5:50; %the times we want
interp1(Time,Progress,TimesWanted)
gives me:
ans =
1.8182 2.7273 3.6364 4.5455 5.4545 6.3636 7.2727 8.1818 9.0909
which is the progress at TimesWanted obtained by interpolation.

MATLAB: What's [Y,I]=max(AS,[],2);?

I just started matlab and need to finish this program really fast, so I don't have time to go through all the tutorials.
can someone familiar with it please explain what the following statement is doing.
[Y,I]=max(AS,[],2);
The [] between AS and 2 is what's mostly confusing me. And is the max value getting assigned to both Y and I ?
According to the reference manual,
C = max(A,[],dim) returns the largest elements along the dimension of A specified by scalar dim. For example, max(A,[],1) produces the maximum values along the first dimension (the rows) of A.
[C,I] = max(...) finds the indices of the maximum values of A, and returns them in output vector I. If there are several identical maximum values, the index of the first one found is returned.
I think [] is there just to distinguish itself from max(A,B).
C = max(A,[],dim) returns the largest elements along the dimension of A specified by scalar dim. For example, max(A,[],1) produces the maximum values along the first dimension (the rows) of A.
Also, the [C, I] = max(...) form gives you the maximum values in C, and their indices (i.e. locations) in I.
Why don't you try an example, like this? Type it into MATLAB and see what you get. It should make things much easier to see.
m = [[1;6;2] [5;8;0] [9;3;5]]
max(m,[],2)
AS is matrix.
This will return the largest elements of AS in its 2nd dimension (i.e. its columns)
This function is taking AS and producing the maximum value along the second dimension of AS. It returns the max value 'Y' and the index of it 'I'.
note the apparent wrinkle in the matlab convention; there are a number of builtin functions which have signature like:
xs = sum(x,dim)
which works 'along' the dimension dim. max and min are the oddbal exceptions:
xm = max(x,dim); %this is probably a silent semantical error!
xm = max(x,[],dim); %this is probably what you want
I sometimes wish matlab had a binary max and a collapsing max, instead of shoving them into the same function...