I'm trying to write this program in MATLAB where, without using loops I need to take an array which has 3 rows, and an unknown number of columns.
We take this array, and find the variance of each of the columns. If that variance is grater than a threshold, then we set a the corresponding column in our output array to 1.
This is what I have so far:
% vec_acc_total is my array with 3 rows
var = var(vec_acc_total, 0, 1);
walking_segments_no_loops = zeros(size(var));
walking_segemnts_no_loops(var >= THRESHOLD) = 1
However something is wrong with this, hoping for any help, will be appreciated. I know it is wrong as after it is returned, It is put through a verification program (provided to me) which will confirm or deny.
There is also potentially an issue with my use of the VARIANCE function in MATLAB. I need to find the variance of each column.
I have tried the following:
walking_segments_no_loops = var >= THRESHOLD
Which also did not return the correct value
When you run
You are overwriting the built-in function
varwith your variablevar. All subsequent calls tovar()(the function) will error until you clearvar(the variable) from your workspace.So run
clearto clean things up and prevent any shadowing, then pick a new variable name, e.g.vis a 1xN array, wherevar_acc_totalwas a 3xN matrix, which tells us intuitively that the variance for each column has been calculated.You can directly check whether this exceeds a threshold without initialising an array:
This outputs a logical array the same size as
v, which istrue (1)when greater than or equal to the threshold andfalse (0)otherwise. If you want this to be numerical instead of logical, you can convert it by wrapping with, for example,double().Note in your question you say this (emphasis mine):
Maybe you want to use
>instead of>=to check for a strict inequality.