How to change to order the subplots? - octave

For any number of rows and columns.
Vertical subplots are down and to the right.
subplot(3,2,i)
1 4
2 5
3 6
{1 4 2 5 3 6}
Horizontal subplots are to the right and down. (default)
subplot(3,2,i)
1 2
3 4
5 6
{1 2 3 4 5 6}
How to get the first (vertical) layout?

For subplot(m,n,..), kv being your vertical linear subplot index, the corresponding usual horizontal one is k = 1 + n*(kv-1) + (1-m*n)*fix((kv-1)/m);
Example:
m = 3; n = 2;
for kv = 1:m*n
k = 1 + n*(kv-1) + (1-m*n)*fix((kv-1)/m);
subplot(m,n, k); title(sprintf("#%d", kv));
end

As far as I know, the default layout is hard-coded and can not be changed. A convenient way to address a particular subplot location is to use its row and column indeces as we would do for a matrix. The formula to get the index i in subplot(n,m,i) at row irow and column icol is:
i = (irow-1)*m+icol
This can be defined in an inline function that you call instead of subplot:
mysubplot = #(n,m,irow,icol) subplot(n,m,(irow-1)*m+icol)
figure(1)
n = 3; m = 2;
for icol = 1:m
for irow = 1:n
mysubplot(n,m,irow,icol); title(sprintf("%d %d", irow,icol));
end
end

Related

Applying 'vector' of functions on a Matlab matrix

Let's say I've got a matrix with n columns, and I've got n different functions.
Is it possible to apply i-th function per each element in i-th column efficiently, that is without using loop?
For example for the following variables:
funs = #(x) [x, cos(x), x.^2]
A = [1 0 1
2 0 2
3 0 3
4 0 4] ;
I would like to obtain the following result:
B = [1 1 1
2 1 4
3 1 9
4 1 16] ;
without looping through columns...

round to the nearest even number with array of numbers

My function and rounding to nearest even number
function y = rndeven(x)
if x<=1
y=2;
else
y = 2*floor(x);
end
endfunction
When I run it I get:
cc=[0:3]'
both=[cc,rndeven(cc)]
0 0
1 2
2 4
3 6
What I'm trying to get as the Result:
0 2
1 2
2 2
3 4
You can use the modulo 2 to find whether a number is even. If it isn't this will return 1, so just add 1 to this number to find the nearest (larger) even number:
function y = rndeven(x)
x = floor(x);
x(x <= 1) = 2;
y = mod(x,2)+x;
end
This works for any array, order of elements does not matter.
You could also check if it is dividable by 2 if you don't want to use the mod function. The pseudo code would be something like this:
while(x % 2 != 0) x = x + 1
return x

Alter row color every x rows on a HTML table

As the question title says, I would like to alter the row colors in a table every x rows.
Not every row, but every x rows.
Just for the sake of explaining, this is what it could look like when altering every 2 rows: JSFiddle
The HTML is just a regular table, something like this:
<table>
...
<tr>
<td>content</td>
<td>more content</td>
</tr>
...
</table>
In the example I'm using a class for the tr's that should be marked, but I want a more generic way, possibly making use of the nth-child selector or something along that way.
Does anybody know of a simple way to do this, possibly with a variable number of altering rows (e.g. every 2 rows, 3 rows, 4 rows,... )? As said, I would like to avoid adding class names because the table would probably be dynamically generated and thus I am most likely not able to alter that code.
You can set colours using nth-child like this:
tr:nth-child(even) {background: #CCC}
tr:nth-child(odd) {background: #FFF}
For more than 2 different options, you use the formula (an + b) where a represents a cycle size, n is a counter (starts at 0), and b is an offset value. Here, we specify a background color for all tr elements whose index is a multiple of 3:
tr:nth-child(3n+0) {background:#999;}
tr:nth-child(3n+1) {background:#CCC;}
tr:nth-child(3n+2) {background:#FFF;}
You're right with nth-child. Just think in multiples/groups of 4:
tr:nth-child(4n+1), tr:nth-child(4n+2) {
background-color: yellow;
/* possibly other styles */
}
Example of the above code
tr:nth-child(4n+1), tr:nth-child(4n+2) {
background-color: gray;
}
4n+1 with n = 0: 4 * 0 + 1 = 1
4n+1 with n = 1: 4 * 1 + 1 = 5
4n+1 with n = 2: 4 * 2 + 1 = 9
4n+1 with n = 3: 4 * 3 + 1 = 13
4n+1 with n = 4: 4 * 4 + 1 = 17
4n+1 with n = 5: 4 * 5 + 1 = 21
The same counts for 4n+2:
4n+2 with n = 0: 4 * 0 + 2 = 2
4n+2 with n = 1: 4 * 1 + 2 = 6
4n+2 with n = 2: 4 * 2 + 2 = 10
4n+2 with n = 3: 4 * 3 + 2 = 14
4n+2 with n = 4: 4 * 4 + 2 = 18
4n+2 with n = 5: 4 * 5 + 2 = 22
Note: n always starts at 0 by default.
You could use an expression for the nth-child of tr as shown below:
tr:nth-child(an+b) { //a and b can be changed to your requirement
background-color: #AAA;
}
Demo here

Trying to find index of minimum value in a matrix fails in Octave

So I have this matrix:
E1 = [54 5 2 4;4 5 19 29;31 4 2 9; 1 3 99 34]
lets say I want to find the location of the value closest to 18.9. let A = 18.9
I would do
[r,c] = find(E1==min(min(abs(E1-A))))
This doesn't work. It returns r = "[](0x1)" and c = "[](0x1)"
however,
if I first do:
F = abs(E1-A) and then do
[r,c] = find(F==min(min(F)))
this gives r = 2 and c = 3 which is correct. 19 is the closest value and 19 lives in row 2 column 3.
Why doesnt this work then? F is simply abs(E1-A) so why can I not put abs(E1-A) in place of F in the find formula?
min(min(abs(E1-A)))
ans = 0.10000
This gives you the min over the absolute difference. Then you compare it to E1 which has absolute values. This is complete different from your second formular
[r,c] = find(F==min(min(F)))
where you comapre the minimum difference with the matrix containing the absolute of differences between E1 and A. If you replace in your second formula F with abs(E1-A) you would get
[r,c] = find(abs(E1-A)==min(min(abs(E1-A))))
Which would also work. Nevertheless I would suggest another approach:
E1 = [54 5 2 4;4 5 19 29;31 4 2 9; 1 3 99 34];
A = 18.9;
# get the index ( Column-major order) of the minimum
idx = nthargout (2, #min, abs (E1-A)(:));
# this returns 10
# convert it ro row, column
[r, c] = ind2sub (size (E1), idx)
r = 2
c = 3

f(n), understanding the equation

I've been tasked with writing MIPS instruction code for the following formula:
f(n) = 3 f(n-1) + 2 f(n-2)
f(0) = 1
f(1) = 1
I'm having issues understanding what the formula actually means.
From what I understand we are passing an int n to the doubly recursive program.
So for f(0) the for would the equation be:
f(n)=3*1(n-1) + 2*(n-2)
If n=10 the equation would be:
f(10)=3*1(10-1) + 2*(10-2)
I know I'm not getting this right at all because it wouldn't be recursive. Any light you could shed on what the equation actually means would be great. I should be able to write the MIPS code once I understand the equation.
I think it's a difference equation.
You're given two starting values:
f(0) = 1
f(1) = 1
f(n) = 3*f(n-1) + 2*f(n-2)
So now you can keep going like this:
f(2) = 3*f(1) + 2*f(0) = 3 + 2 = 5
f(3) = 3*f(2) + 2*f(1) = 15 + 2 = 17
So your recursive method would look like this (I'll write Java-like notation):
public int f(n) {
if (n == 0) {
return 1;
} else if (n == 1) {
return 1;
} else {
return 3*f(n-1) + 2*f(n-2); // see? the recursion happens here.
}
}
You have two base cases:
f(0) = 1
f(1) = 1
Anything else uses the recursive formula. For example, let's calculate f(4). It's not one of the base cases, so we must use the full equation. Plugging in n=4 we get:
f(4) = 3 f(4-1) + 2 f(4-2) = 3 f(3) + 2 f(2)
Hm, not done yet. To calculate f(4) we need to know what f(3) and f(2) are. Neither of those are base cases, so we've got to do some recursive calculations. All right...
f(3) = 3 f(3-1) + 2 f(3-2) = 3 f(2) + 2 f(1)
f(2) = 3 f(2-1) + 2 f(2-2) = 3 f(1) + 2 f(0)
There we go! We've reached bottom. f(2) is defined in terms of f(1) and f(0), and we know what those two values are. We were given those, so we don't need to do any more recursive calculations.
f(2) = 3 f(1) + 2 f(0) = 3×1 + 2×1 = 5
Now that we know what f(2) is, we can unwind our recursive chain and solve f(3).
f(3) = 3 f(2) + 2 f(1) = 3×5 + 2×1 = 17
And finally, we unwind one more time and solve f(4).
f(4) = 3 f(3) + 2 f(2) = 3×17 + 2×5 = 61
No, I think you're right and it is recursive. It seems to be a variation of the Fibonacci Sequence, a classic recursive problem
Remember, a recursive algorithm has 2 parts:
The base case
The recursive call
The base case specifies the point at which you cannot recurse anymore. For example, if you are sorting recursively, the base case is a list of length 1 (since a single item is trivially sorted).
So (assuming n is not negative), you have 2 base cases: n = 0 and n = 1. If your function receives an n value equal to 0 or 1, then it doesn't make sense to recurse anymore
With that in mind, your code should look something like this:
function f(int n):
#check for base case
#if not the base case, perform recursion
So let's use Fibonacci as an example.
In a Fibonacci sequence, each number is the sum of the 2 numbers before it. So, given the sequence 1, 2 the next number is obviously 1 + 2 = 3 and the number after that is 2 + 3 = 5, 3 + 5 = 8 and so on. Put generically, the nth Fibonacci number is the (n - 1)th Fibonacci Number plus the (n - 2)th Fibonacci Number, or f(n) = f(n - 1) + f(n - 2)
But where does the sequence start? This is were the base case comes in. Fibonacci defined his sequence as starting from 1, 1. This means that for our pruposes, f(0) = f(1) = 1. So...
function fibonacci(int n):
if n == 0 or n == 1:
#for any n less than 2
return 1
elif n >= 2:
#for any n 2 or greater
return fibonacci(n-1) + fibonacci(n-2)
else:
#this must n < 0
#throw some error
Note that one of the reasons Fibonacci is taught along with recursion is because it shows that sometimes recursion is a bad idea. I won't get into it here but for large n this recursive approach is very inefficient. The alternative is to have 2 global variables, n1 and n2 such that...
n1 = 1
n2 = 1
print n1
print n2
loop:
n = n1 + n2
n2 = n1
n1 = n
print n
will print the sequence.