octave

Creating an m by n matrix of 0s and 1s from m-sized vector of column indexes

独自空忆成欢 提交于 2019-12-01 17:18:32
I have a m -dimensional vector of integers ranging from 1 to n . These integers are column indexes for m × n matrix. I want to create a m × n matrix of 0s and 1s, where in m -th row there's a 1 in the column that is specified by m -th value in my vector. Example: % my vector (3-dimensional, values from 1 to 4): v = [4; 1; 2]; % corresponding 3 × 4 matrix M = [0 0 0 1; 1 0 0 0; 0 1 0 0]; Is this possible without a for-loop? Of course, that's why they invented sparse matrices: >> M = sparse(1:length(v),v,ones(length(v),1)) M = (2,1) 1 (3,2) 1 (1,4) 1 which you can convert to a full matrix if you

Finding the shortest repetitive pattern in a string

我的未来我决定 提交于 2019-12-01 17:16:47
问题 I was wondering if there was a way to do pattern matching in Octave / matlab? I know Maple 10 has commands to do this but not sure what I need to do in Octave / Matlab. So if a number was 12341234123412341234 the pattern match would be 1234 . I'm trying to find the shortest pattern that upon repetiton generates the whole string . Please note: the numbers (only numbers will be used) won't be this simple. Also, I won't know the pattern ahead of time (that's what I'm trying to find). Please see

vector unpacking for octave

空扰寡人 提交于 2019-12-01 16:56:59
Octave(/matlab)'s notation for handling multiple return values [a, b] = f(x) suggests that the values returned by f(x) are in a sort of row vector and that Octave supports vector unpacking (like Python's tuple-unpacking). Yet when I put [a, b] = [1, 2] I get error: invalid number of output arguments for constant expression Does octave support vector-unpacking? If so, what's the proper notation? I can't find anything in the documentation I don't have Octave to test, but in MATLAB you can "unpack" cell arrays. x = {1 2}; [x1,x2] = x{:} x1 = 1 x2 = 2 You can convert numerical vector to a cell

Conflicting eigen vector outputs between Matlab and Numpy

北城余情 提交于 2019-12-01 16:06:44
I am calculating eigenvectors in Matlab and Numpy, but getting different results. I was under the impression there was only one set of eigenvectors for a given matrix, however both of these outputs seem valid. Here is my matlab code: m = [ 1.4675 + 0.0000i 0.1669 + 1.2654i; 0.1669 - 1.2654i 1.3085 + 0.0000i] [eig_vec,eig_val] = eig(m) eig_val contains: eig_val = 0.1092 0 0 2.6668 eig_vec contains: eig_vec = 0.0896 + 0.6789i 0.0953 + 0.7225i -0.7288 + 0.0000i 0.6848 + 0.0000i Here is my python code: m = np.array([[1.46753694+0.j, 0.16692111+1.26535838j], [0.16692111-1.26535838j, 1.30851770+0.j]

Conflicting eigen vector outputs between Matlab and Numpy

拟墨画扇 提交于 2019-12-01 15:48:51
问题 I am calculating eigenvectors in Matlab and Numpy, but getting different results. I was under the impression there was only one set of eigenvectors for a given matrix, however both of these outputs seem valid. Here is my matlab code: m = [ 1.4675 + 0.0000i 0.1669 + 1.2654i; 0.1669 - 1.2654i 1.3085 + 0.0000i] [eig_vec,eig_val] = eig(m) eig_val contains: eig_val = 0.1092 0 0 2.6668 eig_vec contains: eig_vec = 0.0896 + 0.6789i 0.0953 + 0.7225i -0.7288 + 0.0000i 0.6848 + 0.0000i Here is my python

Custom x-axis values in a matlab plot

∥☆過路亽.° 提交于 2019-12-01 15:23:27
问题 Currently when I plot a 9 by 6 array, the x-axis of the figure is just 1, 2, 3 up to 9. The Y-axis shows the correct values. Instead of 1 to 9 I would like the x-axis values to be custom. They should be 100 200 400 1000 2000 5000 10000 20000 50000 instead. I tried set(gca,'XTick', [100 200 400 1000 2000 5000 10000 20000 50000]) But that's not the correct way to do it. Is there a Matlab option to have these custom values for the x-axis? Why is Matlab just using 1 to 9 anyway? 回答1: You should

Undefined argument when declaring function in Octave

时间秒杀一切 提交于 2019-12-01 15:08:13
问题 I get undefined variable/argument when trying to define my own random generator function. Code: function result = myrand(n, t, p, d) a = 200 * t + p big_rand = a * n result = big_rand / 10**d return; endfunction mrand = myrand(5379, 0, 91, 4) error: >> myrand error: 't' undefined near line 2 column 15 error: called from myrand at line 2 column 7 回答1: You can't start a script with a function keyword. https://www.gnu.org/software/octave/doc/v4.0.1/Script-Files.html This works: disp("Running..."

Splitting a DTMF signal from a wav file using Matlab

偶尔善良 提交于 2019-12-01 14:15:29
Here is the context of the problem: I have a DTMF signal in wav format, I have to identify the number sequence it has encoded. I must do so using fast fourier transform in Matlab, implying that I read the wav file using wavread and to identify each number that is seperated by 40ms silence or more. Here is my code so far: [signal, fs] = wavread( 'C:\Temp\file.wav' ); % here, fs = 8000Hz N = 512; T = 1/fs; L = length( signal ) samples = fs / 1000 * 40 windows = floor(L / samples) t = (1:L)/fs; figure(1), plot(t, signal); Here is what the figure 1 looks like, that is the signal read from the wav:

Return subscripts of a variable dimension matrix

爱⌒轻易说出口 提交于 2019-12-01 10:57:02
I need to be able to return the indices [x1 x2 ... xd] of the elements of a matrix of dimensions LxLxL..xL. The number of dimensions d is a variable supplied to my function. The matrix does not exist, instead I have a linear array of length L^d. For a given index i in the array, I would like to know the equivalent indices in the matrix. I can already do this using a simple for loop, but I am curious to know if I could use ind2sub somehow. The problem I have is that if I do x=zeros(1,d) x=ind2sub(L,i) x is reassigned a single number and not an array of all subscripts. Is there a way of doing

Return subscripts of a variable dimension matrix

佐手、 提交于 2019-12-01 09:21:19
问题 I need to be able to return the indices [x1 x2 ... xd] of the elements of a matrix of dimensions LxLxL..xL. The number of dimensions d is a variable supplied to my function. The matrix does not exist, instead I have a linear array of length L^d. For a given index i in the array, I would like to know the equivalent indices in the matrix. I can already do this using a simple for loop, but I am curious to know if I could use ind2sub somehow. The problem I have is that if I do x=zeros(1,d) x