"Out of Bounds" Error while calculating the mean gene - octave

This is my first time trying to complete work in Octave. I have attempted to complete "for loops" to get the mean of each and then subtract this to centre the results in the 25 samples of the 5 items. I get the right figures, however I also get an out of bounds error (indicated below). Can anyone help me please?
error: TrialPartB: A(I,J): row index out of bounds; value 6 out of bound 5

You have populated your G_all structure with only 5 data members, but then, when you calculate the mean, you loop i=1:25. There are only 5 members, so when it gets to member 6, it fails with the 'row index out of bounds' error.
You need to limit the for loop to be just the size of the data, perhaps using rows(G_all) instead of 25 as the limit of the loop.

As rolfl already explained you are trying to access row 1..25 but G_all only has 5 rows.
But apart that problem you shouldn't calculate mean in a for loop but use the function "mean" instead.
a=[4 1 6];
mean(a)
ans = 3.6667
If you want to remove the mean from an vector just use "detrend":
detrend(a, 0)
ans =
0.33333 -2.66667 2.33333

Related

Octave out of bound error while trying to calculate mean value of a vector

I generated random values using following function :
P = floor(6*rand(1,30)+1)
Then, using T=find(P==5), I got values where outcome is 5 and stored them in T. The output was :
T =
10 11 13 14 15 29
Now, I want to calculate the mean value of T using mean(T) but it gives me following error :
error: mean(29): out of bound 1 (dimensions are 1x1) (note: variable 'mean' shadows function)
What I am trying to do is to model the outcomes of a rolling a fair dice and counting the first time I get a 5 in outcomes. Then I want to take mean value of all those times.
Although you don't explicitly say so in your question, it looks like you wrote
mean = mean(T);
When I tried that, it worked the first time I ran the code but the second and subsequent times it gave the same error that you got. What seems to be happening is that the first time you run the script it calculates the mean of T, which is a scalar, i.e. it has dimensions 1x1, and then stores it in a variable called mean, which then also has dimensions 1x1. The second time you run it, the variable mean is still present in the environment so instead of calling the function mean() Octave tries to index the variable called mean using the vector T as the indices. The variable mean only has one element, whose index is 1, so the first element of T whose value is different from 1 is out of bounds. If you call your variable something other than mean, such as, say, mu:
mu = mean(T);
then it should work as intended. A less satisfactory solution would be to write clear all at the top of your script, so that the variable mean is only created after the function mean() has been called.

Engineering Equation Solver - Functions

I must calculate a function on EES.
Function: T(t)=((T_surface-T_infinity)*(e^(-bt)))+T_infinity
t is time and limits are between 1 and 40 second. I need calculate every seconds in 1-40.
How can I write this function in EES?
If I understand the question correctly, no differential equation should be solved, no integration, summation or the like should be carried out. Only a calculation with variation of the variable t is to be accomplished.
There are two possibilities. But first: EES is not 'key-sensitive'! So you should choose one—T or t—and the other should get another 'name'. I prefer 'tau' for time.
Parametric table
Write the equation in the equation window and aad the parameter you may have(?). Do not define 'tau'. like:
T=((T_surface-T_infinity)*(exp(-b*tau)))+T_infinity
T_surcace = 80[C]
T_infinity = 20[C]
b=3
Then open the parametic table, add the variables you want to see. At least you should take T and tau. Expand the rows of the table to 40 and enter the respective times. Then press the green play-button (top left in the table).
Duplicate function:
T_surface = 80[C]
$varinfo tau[] units='s'
b=0.1[1/s]
Duplicate i=1,40
tau[i] = i*1[s]
T[i]=((T_surface-T_infinity)*(exp(-b*tau[i] )))+T_infinity
End
T_infinity = 20[C]
$varinfo T[] units='C'

For-Loop for finding combinations of springs?

I need to use a for-loop in a function in order to find spring constants of all possible combinations of springs in series and parallel. I have 5 springs with data therefore I found the spring constant (K) of each in a new matrix by using polyfit to find the slope (using F=Kx).
I have created a function that does so, however it returns data not in a matrix, but as individual outputs. So instead of KP (Parallel)= [1 2 3 4 5] it says KP=1, KP=2, KP=3, etc. Because of this, only the final output is stored in my workspace. Here is the code I have for the function. Keep in mind that the reason I need to use the +2 in the for loop for b is because my original matrix K with all spring constants is ten columns, with every odd number being a 0. Ex: K=[1 0 2 0 3 0 4 0 5] --- This is because my original dataset to find K (slope) was ten columns wide.
function[KP,KS]=function_name1(K)
L=length(K);
c=1;
for a=1:2:L
for b=a+2:2:L
KP=K(a)+K(b)
KS=1/((1/K(a))+(1/K(b)))
end
end
c=c+1;
and then a program calling that function
[KP,KS]=function_name1(K);
What I tried: - Suppressing and unsuppressing lines of code (unsuccessful)
Any help would be greatly appreciated.
hmmm...
your code seems workable, but you aren't dealing with things in the most practical manner
I'd start be redimensioning K so that it makes sense, that is that it's 5 spaces wide instead of your current 10 - you'll see why in a minute.
Then I'd adjust KP and KS to the size that you want (I'm going to do a 5X5 as that will give all the permutations - right now it looks like you are doing some triangular thing, I wouldn't worry too much about space unless you were to do this for say 50,000 spring constants or so)
So my code would look like this
function[KP,KS]=function_name1(K)
L=length(K);
KP = zeros(L);
KS = zeros(l);
c=1;
for a=1:L
for b=1:L
KP(a,b)=K(a)+K(b)
KS(a,b)=1/((1/K(a))+(1/K(b)))
end
end
c=c+1;
then when you want the parallel combination of springs 1 and 4 KP(1,4) or KP(4,1) will do the trick

Unexpected results of find function in matlab

t=find(str.tubetime >= str.time,1);
assume tubetime is a matrix of 1 x 1001 elements
assume time is a double =0.0012
From what I understand of the code is it finds the first value of the tubetime matrix which is
of equal or greater value returning the index of where this value is found in tubetime.
If I am correct, why am I getting an index value of 244. When the value of 0.0012 is contained at index points starting at 231 through to the index point 250.
Edit:
I have just double checked my variables are accurate, as I am currently in debug mode, and reading it back from the system. Thank you for your input, do you have any idea what could be wrong with it?
Here is a screenshot showing the values
When you view the values in printscreen it is probably cutting off after the 4th decimal place. See my comment above on your original post.
Your description of FIND is correct, but one of your variables is not as you described it. e.g.,
t=find([1 1 2 3 4 5 6] >= 3,1)
returns 4, as it should.
You have specifically asked that it should return only one element in your syntax
time = zeros(1,1001);
time(231:250) = 0.0012 % setting an array where indices 231 - 250 are 0.0012 else is zero
find(time>=0.0012)
% gives all indices
find(time>=0.0012,1)
%returns 231 only
find(time>=0.0012,2)
%returns 231,232
PLUS check that the values are not shown in short format, that is they are 0.001199 but shown as 0.0012.

How to do multiple boxplots with Octave?

I have 3 matrices that I would like to be plotted on a boxplot (two of them are 22 rows by 83 columns, and the other is 7 rows by 83 columns) within Octave.
I've tried:
boxplot([red(:,1),blue(:,1),purple(:,1)])
error: horizontal dimensions mismatch
error: evaluating argument list element number 1
But, I keep getting the above error. I assume it's because I have one matrix with 7 rows instead of 22? If so, is there any possible way of getting them both plotted on the same boxplot?
When you pass [a,b,c] you are trying to build a matrix by concatenating horizontally the other three. Since they do not have the same number of rows that will never work.
If you want to do the boxplot use cells (as indicated in help boxplot) that is
boxplot ({red(:,1),blue(:,1),purple(:,1)})