I'm trying to insert a new row at the beginning of a matrix, but the result is inserting my row vector rotated:
a: (.7 .3; .1 .2)
b: (.5 .5)
b, a
0.5
0.5
0.7 0.3
0.1 0.2
Intended result:
0.5 0.5
0.7 0.3
0.1 0.2
What am I doing wrong?
(enlist b), a gives the result you want. It helps to think of a as being made from nested lists, hence any new rows should be of this form as well.
Or you can make b a matrix. Join on matrices works the way you expect.
q)(1 2#b),a
0.5 0.5
0.7 0.3
0.1 0.2
Related
I have the following probability transition matrix with each row corresponding to letters A, B, C, and D. I am trying to write a Huffman code for A, B, C, and D using these transition probabilities. I tried both by hand and in Matlab but I am confused whether or not you would make the code for AA, AB, AD, BB, ... etc or somehow account for all the probabilities and just find a code for A, B, C, and D. I do not think that this Matlab code gives the codes I am looking for. Any suggestions would be great.
T=[0.7 0.2 0 0.1;
0 0.8 0 0.2;
0.7 0.1 0.2 0;
0 0 0.6 0.4] %Probability Transition Matrix
p1 = [.7 .2 0 0.1];
p2 = [0 0.8 0 0.2];
p3 = [0.7 0.1 0.2 0];
p4 = [0 0 0.6 0.4];
%Create a Huffman dictionary based on the symbols and their probabilities.
dict1 = huffmandict(symbols,p1);
dict2 = huffmandict(symbols,p2);
dict3 = huffmandict(symbols,p3);
dict4 = huffmandict(symbols,p4);
You would have four codes, as you are constructing them, where the code used for the next symbol is selected by the previous symbol. (The first symbol would simply be sent as is.)
Note that only four symbols, or really two or three for each of your codes, is not much for Huffman coding to work with. In fact, there is only one possible Huffman code for each of the two or three symbol cases.
I was trying to find out, how to plot a cumulative distribution function (cdf) with specific x values but was not successful.
For example, if the dataset is:
x = [2.50 5.21 7.67 8.43 9.15 11.47 14.59 21.45];
y = [0.20 0.09 0.15 0.13 0.17 0.04 0.7 0.15]; % (total 1)
the graph shape definitely looks wrong, when I use y = cdfplot(x).
I also plotted the graph with cumsum(y) and x to check the shape and it looks fine, but I would like to know, if there is any code which plots cumulative distribution plots.
There's the stairs function for creating "stairstep graphs", which should be exactly what you want, incorporating your cumsum(y) idea.
Please see the following code snippet. I added two additional points for the start and end of some interval, here [0 ... 25]. Also, your values in y sum up to something larger than 1, so I modified these values, too.
x = [0 2.50 5.21 7.67 8.43 9.15 11.47 14.59 21.45 25];
y = [0 0.10 0.09 0.05 0.10 0.14 0.04 0.4 0.08 0];
stairs(x, cumsum(y));
xlim([-1 26]);
ylim([-0.2 1.2]);
That'd be the output (Octave 5.1.0, but also tested with MATLAB Online):
Hope that helps!
Let's say we have
[x]=[0.1 0.2 0.3 0.4]
[y]=[0.25 0.30 0.40 0.55]
y1=diff(y)./diff(x)
y2=diff(y1)./diff(x)
And the result I get is
Matrix dimensions must agree
How do I solve this problem?
I redirect you towards this documentation. When you use the diff function, it will actually return you a vector with m-1 (m being its length), since what it does is output this:
diff(y1) = [y1(2)-y1(1) y1(3)-y1(2) ... y1(m)-y(m-1)]
As you can see, you will loose one value, and thus explaining your error. When you do your last line, it cannot divide diff(y1) by diff(x) since diff(y1) is equal to a vector of length 2 and diff(x) is equal to a vector of length 3.
Depending on what you want to do, you can change the code to the following :
[x]=[0.1 0.2 0.3 0.4]
[y]=[0.25 0.30 0.40 0.55]
y1=diff(y)./diff(x)
y2=diff(y1)./diff(x(1:end-1))
If you want to approximate the derivate of y, I really suggest you to take a look at the example in the page I linked. The matlab documentation always gives examples on how to use their functions, so go take a look. According to the documentation, if you want to calculate the partial derivate of the vector y, you need the step of your x vector.
x=[0.1 0.2 0.3 0.4]
y=[0.25 0.30 0.40 0.55]
x_step = 0.1
y1=diff(y)./x_step
y2=diff(y1)./x_step
x=[0.1 0.2 0.3 0.4] ;
y=[0.25 0.30 0.40 0.55] ;
dy = gradient(y)./gradient(x) ;
d2y = gradient(dy)./gradient(x) ;
I have a vector with the size (1,9) with the value as follows:
V= [0.5 0.1 0.1 0.9 0.5 0.1 0.9 0.9 0.5]
How can I convert the vector V into the matrix M with the size of (3,3) which the first row is the first 3 elements of the vector and the second row contains the next 3 elements of the vector and keeping that rule for all of other elements of the vector as follows:
0.5 0.1 0.1
M= 0.9 0.5 0.1
0.9 0.9 0.5
Also for different sized vectors, like for example (1,100), how can I convert into a matrix of (10,10) base on the rule above?
Use reshape, then transpose the result:
M = reshape(V, 3, 3).';
reshape transforms a vector into a matrix of a desired size. The matrix is created in column-major order. Therefore, just using reshape by itself will place the elements in the columns. Since you desire the elements to be populated by rows, a trick is to simply transpose the result.
In general, you want to reshape a N element vector V into a square matrix M of size sqrt(N) x sqrt(N) in row-major order. You can do this for the general case:
N = sqrt(numel(V));
M = reshape(V, N, N).';
This of course assumes that the total number of elements in V is a perfect square.
I wanted to fit an arbitrary function ( (k_plus-k_t*(1-exp(-k_plus/(a*k_t+b*k_d)))-k_d*(exp(-k_plus/(a*k_t+b*k_d)) to my data set. Therefore, I used lsqcurvefit in MATLAB.
The code was as follow:
clc;
clear all;
close all;
%% assign the anon function to a handle
k_plus =[0.1 0.2 0.4 0.7 1 1.1 1.2 1.5 1.7 2 2.5 3 3.5 4 5];
K_minus_d = [0.1 0.2 0.4 0.7 1 1.1 1.2 1.5 1.7 2 2.5 3 3.5 4 5];
K_minus_t =[ 0.1 0.2 0.4 0.7 1 1.1 1.2 1.5 1.7 2 2.5 3 3.5 4 5];
f1= sprintf('table%02d.txt',1);
data=tblread(f1);
x1=data(:,1);
x1=x1';
F=#(c,xdata)(xdata-K_minus_t*(1-exp(-xdata/(c(1)*K_minus_t+c(2)* K_minus_d)))- K_minus_d*(exp(-xdata/(c(1)*K_minus_t+c(2)* K_minus_d)))
x0 = [0.1 0.1];
[c,resnorm,~,exitflag,output] = lsqcurvefit(F,x0,k_plus,x1)
figure;
hold on
plot(k_plus,x1,'r-', 'LineWidth', 1)
plot(k_plus,F(c,k_plus),'-b*','LineWidth', 1,'MarkerSize', 1)
hold off
grid on;
I wonder how can I select the x0 (initial point for x) because I got different value for C when I change it
If I understand you correctly, you're getting different values for c depending on your choice of the initial x0 and you want advice on how to choose a good initial value.
This is something that is hard to answer without sufficient knowledge about the function you are trying to fit. Is it describing experimental data? If so, you should have some expectations about what x should approximately be.
But it can very well be the case that there is no unique solution for your equation or that the solver gets stuck in a local minimum.