octave

GROUP BY in MATLAB

♀尐吖头ヾ 提交于 2019-12-23 08:10:16
问题 I want to do what SQL's GROUP BY does in MATLAB. For example, M = [ 1, 5; 2, 5; 3, 5; 1, 6; 2, 6; 1,7 ] SQL: SELECT MAX(c1), c2 FROM M(c1, c2) GROUP BY 2 Result = [ 3, 5; 2, 6; 1, 7] How can I do this in Matlab? 回答1: grpstats in the Statistics Toolbox can do this: >> [grpstats(M(:,1), M(:,2), {'max'}) unique(M(:,2))] ans = 3 5 2 6 1 7 回答2: If you don't mind doing some preprocessing to get the order (or if the first column is nicely built from 1 to n ), you can do it like this: accumarray([1 2

matlab/octave - Generalized matrix multiplication

夙愿已清 提交于 2019-12-23 07:27:25
问题 I would like to do a function to generalize matrix multiplication. Basically, it should be able to do the standard matrix multiplication, but it should allow to change the two binary operators product/sum by any other function. The goal is to be as efficient as possible, both in terms of CPU and memory. Of course, it will always be less efficient than A*B, but the operators flexibility is the point here. Here are a few commands I could come up after reading various interesting threads: A =

calc digits/decimal places from resolution

匆匆过客 提交于 2019-12-23 05:49:26
问题 Is there an easy way to calculate the number of digits needed to display a floating point number with a given resolution? (Without going through string conversion) resolution = [0.1 0.01 0.05 0.025 0.10001]; % first try digits = -floor (log10 (resolution)) % wanted output ex_digits = [1 2 2 3 5]; gives digits = 1 2 2 2 1 The first three results are fine but the other fails with my first attempt. 回答1: You can multiply the number by powers of 10 and compare the result with its floor. resolution

vectorizing a matlab / octave FOR loop

送分小仙女□ 提交于 2019-12-23 04:49:26
问题 I'm a little confused as to how to vectorize this for loop see code below: array1=[xfreq_orig,yamp_orig,yamp_inv,phase_orig] %frequency, amplitudes, phases to use t_rebuilt=linspace(0,2*pi,44100) aa_sig_rebuilt_L=zeros(1,length(t_rebuilt)); aa_sig_combined_L=zeros(1,length(t_rebuilt)); sig_full_L=zeros(1,length(t_rebuilt)); for kk=1:1:numel(xfreq_orig); aa_sig_rebuilt_L = array1(kk, 2)*cos ((array1(kk,1))*t_rebuilt+(array1(kk, 4))); aa_sig_combined_L = aa_sig_combined_L + aa_sig_rebuilt_L;

Loading hyperspectral images in Octave

帅比萌擦擦* 提交于 2019-12-23 02:48:31
问题 Using Matlab I can see the 4th channel of my image, but not with Octave: the following Matlab code for image (I) gives I =imread('../rec3/2012R_1_1.tif') [nr nc nb] = size(I); nb It gives out : nb = 4 where nb is the channel number and I is a 64 bit image(3057x1210) But for the same image Octave gives : nb = 3 Should I load any special package in order to be able to work with hyperspectral images? EDIT The output of the imfinfo for Octave: ans = scalar structure containing the fields:

Octave code for gradient descent using vectorization not updating cost function correctly

倾然丶 夕夏残阳落幕 提交于 2019-12-23 02:38:08
问题 I have implemented following code for gradient descent using vectorization but it seems the cost function is not decrementing correctly.Instead the cost function is increasing with each iteration. Assuming theta to be an n+1 vector, y to be a m vector and X to be design matrix m*(n+1) function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters) m = length(y); % number of training examples n = length(theta); % number of features J_history = zeros(num_iters, 1); error = ((theta'

Numerical grouping using matlab / octave and not repeating values found in main array

此生再无相见时 提交于 2019-12-23 02:31:24
问题 I have about 100,000 numbers that I would like to group together based on dividing by two or increments of two. PS: The increment values may change and the values found in the main array "x" can only be used once. I'm not sure how to check and stop the loop if a number in the "array_all" array has been repeated from the "x" array. See example below Example: x=[9,8,7,6,5,4,3,2,1] I'm trying to get the array_all array to look like this: array_all= [ 9.00000 4.50000 2.25000 8.00000 4.00000 2

How to get Octave's plot to work under emacs?

和自甴很熟 提交于 2019-12-23 01:54:12
问题 Using octave in a terminal window x=1:10; plot(x); works fine, produces a plot window which is zoomable etc. running octave under emacs, the plot appears, but the plot window is hung and you can't zoom. If you execute plot(x); again, the window disappears. I'm using Debian Jessie, with GNU Emacs 24.3.1 and GNU Octave, version 3.8.1. To get it to work at all I had to make a .octaverc to set the prompt. $ cat ~/.octaverc PS1(">> ") 回答1: It seems that the problem is caused by octave-mode running

matlab: fmincon, pass variables into nonlcon

拈花ヽ惹草 提交于 2019-12-22 17:45:21
问题 I know its a stupid question, but I have no idea how to solve it... Lets say I have something like: x = fmincon(@myfun,x0,A,b,Aeq,beq,lb,ub,@mycon) and later on : function [c,ceq] = mycon(x) c = ... ceq = ... How to pass additional variables into @mycon , such as function [c,ceq] = mycon(x, variable) if variable == 1 c = ... ceq = ... else c = ... ceq = ... end Thanks :) 回答1: You pass mycon as anonymous function: x = fmincon(@myfun,x0,A,b,Aeq,beq,lb,ub,@(xx)mycon(xx,variable)) Note that

how to add tex to octave graph

泪湿孤枕 提交于 2019-12-22 14:59:22
问题 I have a bar graph, and I am using text() to display fractions above each of the bars. For example: text(1, 20, "300/400") displays the string "300/400" at position (1,20). I would now like to prettify the fractions so that "300" appears over the "400": 300 --- 400 Is there a way to do this, eg with tex or latex? I tried each of these, but the interpreter is not getting called: text(1, 20, '\frac{300}{400}'); text(1, 20, '\frac{300}{400}', 'interpreter', 'tex'); text(1, 20, '\frac{300}{400}',