Accessing from 2nd row of the matrix in octave? - octave

I am new to octave and learning it.
Suppose I have a matrix X =
1 2
3 4
5 6
I want to access this matrix from second row, omitting the first row.
What is the syntax for it!?
I could delete the row by X(1,:) = [] which will change the original matrix,
How to access from the second row in octave?

Use colon syntax. To return row 2 to the end use:
X(2:end, :)
See GNU Octave documentation for more indexing options.

Related

Automatic broadcasting warning: How to compare matrix rows to vector in Octave

I'm getting the warning:
warning: mx_el_eq: automatic broadcasting operation applied
From the code:
f = [1;2;3];
f == 1:3;
warning: mx_el_eq: automatic broadcasting operation applied
Can this can be done without warnings?
This is because you are comparing column vector f with row vector 1:3. In Matlab this would be an error however Octave automatically broadcasts. This means that in order to apply the == operator it will expand one of your vectors along a singleton dimension (i.e. a dimension of size 1). In you case both vectors have a singleton dimension to expand so you essentially get the equivalent of:
a1 = [1 1 1;
2 2 2;
3 3 3]; %// for f
a2 = [1 2 3
1 2 3
1 2 3]; %// for 1:3
a1 == a2
Note that in order to get the same result in Matlab you would have to directly call bsxfun
bsxfun(#eq, f, 1:3)
In order to compare you vectors elementwise without the broadcasting you just need to transpose one of them:
f' == 1:3
Automatic broadcasting was a new feature introduced in Octave 3.6. It surprised many people (which were expecting an error), so it was decided to throw a warning. To disable this warning you'll need to turn it off with:
warning ("off", "Octave:broadcast");
You can also turn it off only in the scope of your function:
warning ("off", "Octave:broadcast", "local");
However, I'd recommend you do it in your .octaverc file instead.
The problem with the decision of throwing a warning is that it sounds like you are doing something wrong when you're really not. So as of Octave 4.0, that warning got removed (it is now part of the "Octave:language-extension" warning id).

"Out of Bounds" Error while calculating the mean gene

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

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

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

The most efficient way to calculate an integral in a dataset range

I have an array of 10 rows by 20 columns. Each columns corresponds to a data set that cannot be fitted with any sort of continuous mathematical function (it's a series of numbers derived experimentally). I would like to calculate the integral of each column between row 4 and row 8, then store the obtained result in a new array (20 rows x 1 column).
I have tried using different scipy.integrate modules (e.g. quad, trpz,...).
The problem is that, from what I understand, scipy.integrate must be applied to functions, and I am not sure how to convert each column of my initial array into a function. As an alternative, I thought of calculating the average of each column between row 4 and row 8, then multiply this number by 4 (i.e. 8-4=4, the x-interval) and then store this into my final 20x1 array. The problem is...ehm...that I don't know how to calculate the average over a given range. The question I am asking are:
Which method is more efficient/straightforward?
Can integrals be calculated over a data set like the one that I have described?
How do I calculate the average over a range of rows?
Since you know only the data points, the best choice is to use trapz (the trapezoidal approximation to the integral, based on the data points you know).
You most likely don't want to convert your data sets to functions, and with trapz you don't need to.
So if I understand correctly, you want to do something like this:
from numpy import *
# x-coordinates for data points
x = array([0, 0.4, 1.6, 1.9, 2, 4, 5, 9, 10])
# some random data: 3 whatever data sets (sharing the same x-coordinates)
y = zeros([len(x), 3])
y[:,0] = 123
y[:,1] = 1 + x
y[:,2] = cos(x/5.)
print y
# compute approximations for integral(dataset, x=0..10) for datasets i=0,1,2
yi = trapz(y, x[:,newaxis], axis=0)
# what happens here: x must be an array of the same shape as y
# newaxis tells numpy to add a new "virtual" axis to x, in effect saying that the
# x-coordinates are the same for each data set
# approximations of the integrals based the datasets
# (here we also know the exact values, so print them too)
print yi[0], 123*10
print yi[1], 10 + 10*10/2.
print yi[2], sin(10./5.)*5.
To get the sum of the entries 4 to 8 (including both ends) in each column, use
a = numpy.arange(200).reshape(10, 20)
a[4:9].sum(axis=0)
(The first line is just to create an example array of the desired shape.)