Mat file loads slower than m-file - performance
I have a large variable stored in a mat file and in a .m file.
The data is just a big cell:
Tensor{1,1,1,1,1,1,1,1,1,1,1,1}=[1,1,2,2,1, (... )];
Tensor{1,1,1,1,1,1,1,1,1,1,1,2}=[1,1,2,2,3, (... )];
(...)
Why it is so much slower to load the variable form the mat-file, than from the m-file?
tic;load('Tensor.mat');toc
Elapsed time is 6.969654 seconds.
tic;Tensor;toc
Elapsed time is 0.152476 seconds.
Is there a way to save variables as m-files?
#Daniel An example of a typical entry is:
Tensor{2,2,2,2,2,4,4,4,4,4,4,4} = [ ...
0,0,0,0,0,(1 / 6) .* 22 .^ (-1 / 2),0,0,0,0,0,0,0 ; ...
0,0,0,0,(1 / 6) .* ( 5 / 33) .^ (1 / 2),0,(-1 / 12) .* (7 / 11) .^ (1 / 2),0,0,0,0,0,0 ; ...
0,0,0, (1 / 11) .* (5 / 6) .^ (1 / 2),0,(-35 / 132) .* 3 .^ (-1 / 2),0,(7 / 44) .* 3 .^ (-1 / 2),0,0,0,0,0; ...
0,0,(1 / 11) .* (5 / 6) .^ (1 / 2),0,(-7 / 33),0,(1 / 44) .* 105 .^ (1 / 2),0,(-7 / 66),0,0,0,0; ...
0,(1 / 6) .* (5 / 33) .^ (1 / 2),0,(-7 / 33),0,(7 / 33) .* (5 / 2) .^ (1 / 2),0,(-35 / 198) .* (5 / 2) .^ (1 / 2),0,(7 / 66),0,0,0; ...
(1 / 6) .* 22 .^ (-1 / 2),0,(-35 / 132) .* 3 .^ (-1 / 2),0,(7 / 33) .* (5 / 2) .^ (1 / 2),0,(-25 / 66) .* (7 / 6) .^ (1 / 2),0,(35 / 198) .* (5 / 2) .^ (1 / 2),0,(-7 / 44) .* 3 .^ (-1 / 2),0,0; ...
0,(-1 / 12) .* (7 / 11) .^ (1 / 2),0,(1 / 44) .* 105 .^ (1 / 2),0,(-25 / 66) .* (7 / 6) .^ (1 / 2),0,(25 / 66) .* (7 / 6) .^ (1 / 2),0,(-1 / 44) .* 105 .^ (1 / 2),0,(1 / 12) .* (7 / 11) .^ (1 / 2),0; ...
0,0,(7 / 44) .* 3 .^ (-1 / 2),0,(-35 / 198) .* (5 / 2) .^ (1 / 2) ,0,(25 / 66) .* (7 / 6) .^ (1 / 2),0,(-7 / 33) .* (5 / 2) .^ (1 / 2),0,( 35 / 132) .* 3 .^ (-1 / 2),0,(-1 / 6) .* 22 .^ (-1 / 2); ...
0,0,0,(-7 / 66), 0,(35 / 198) .* (5 / 2) .^ (1 / 2),0,(-7 / 33) .* (5 / 2) .^ (1 / 2),0,( 7 / 33),0,(-1 / 6) .* (5 / 33) .^ (1 / 2),0; ...
0,0,0,0,(7 / 66),0,(-1 / 44) .* 105 .^ (1 / 2),0,(7 / 33),0,(-1 / 11) .* (5 / 6) .^ (1 / 2),0,0; ...
0,0,0,0,0,(-7 / 44) .* 3 .^ (-1 / 2),0,(35 / 132) .* 3 .^ (-1 / 2),0,(-1 / 11) .* (5 / 6) .^ (1 / 2),0,0,0; ...
0,0,0,0,0,0,(1 / 12) .* (7 / 11) .^ (1 / 2),0,(-1 / 6) .* (5 / 33) .^ (1 / 2),0,0,0,0; ...
0,0,0,0,0,0,0,(-1 / 6) .* 22 .^ (-1 / 2),0,0,0,0,0 ...
] ;
That is because the .mat-file is a binary file that requires some processing to extract a variable, while the .m-file is nothing else just a text file. Instead of .m-file it is more convenient to save data in .dat-file. The result should be about the same. This can be checked trying these two commands:
tic;load('Tensor.mat');toc
tic;load('Tensor.dat','-ascii');toc
Related
Object ellipticity based on the moment invariants - Matlab
Following a question about Hu moments and a question regarding object Ellipse Variance I used the code from this link to try to calculate ellipticity based on the moment invariants like in the image below (Based on A Beginner’s Guide to Image Shape Feature Extraction Techniques). I the code I used (only checking the first 2 objects) I do not get a value close to 1 (which should indicate an Ellipticity) when I have an ellipse object. Can someone please explain what I did wrong? code: clc; clear; close all; % Image processing I= rgb2gray(imread('https://az877327.vo.msecnd.net/~/media/images/products/2005/other/mipix%20ellipses%20full%20frame.jpg?v=1&h=550&w=800&crop=1')); bw = imbinarize(I); bw = imfill(bw,'holes'); bw = bwareaopen(bw, 100); imshow(bw) hold on; [B,L] = bwboundaries(bw,'noholes'); stats = regionprops(L,'Centroid','Image'); %Loop - data acquisition for i = 1 : 2 %numel(stats) b = B{i}; c = stats(i).Centroid; f = stats(i).Image; y = b(:,1); x = b(:,2); plot( b(:,2),b(:,1),'Color','red','linewidth',1); text(c(1),c(2),num2str(i),'Color','red'); %based on https://raw.githubusercontent.com/dipum/dipum-toolbox/master/dipum/invmoments.m [M,N] = size(f); [x,y] = meshgrid(1:N,1:M); % Turn x,y, and F into column vectors to make the summations a bit % easier to compute in the following. x = x(:); y = y(:); f = f(:); % DIPUM3E equation (13-27) m.m00 = sum(f); % Protect against divide-by-zero warnings. if (m.m00 == 0) m.m00 = eps; end % The other central moments: m.m10 = sum(x .* f); m.m01 = sum(y .* f); m.m11 = sum(x .* y .* f); m.m20 = sum(x.^2 .* f); m.m02 = sum(y.^2 .* f); m.m30 = sum(x.^3 .* f); m.m03 = sum(y.^3 .* f); m.m12 = sum(x .* y.^2 .* f); m.m21 = sum(x.^2 .* y .* f); % DIPUM3E equations (13-28) through (13-30). xbar = m.m10 / m.m00; ybar = m.m01 / m.m00; e.eta11 = (m.m11 - ybar*m.m10) / m.m00^2; e.eta20 = (m.m20 - xbar*m.m10) / m.m00^2; e.eta02 = (m.m02 - ybar*m.m01) / m.m00^2; e.eta30 = (m.m30 - 3 * xbar * m.m20 + 2 * xbar^2 * m.m10) / ... m.m00^2.5; e.eta03 = (m.m03 - 3 * ybar * m.m02 + 2 * ybar^2 * m.m01) / ... m.m00^2.5; e.eta21 = (m.m21 - 2 * xbar * m.m11 - ybar * m.m20 + ... 2 * xbar^2 * m.m01) / m.m00^2.5; e.eta12 = (m.m12 - 2 * ybar * m.m11 - xbar * m.m02 + ... 2 * ybar^2 * m.m10) / m.m00^2.5; % DIPUM3E Table 13.8. phi(1) = e.eta20 + e.eta02; phi(2) = (e.eta20 - e.eta02)^2 + 4*e.eta11^2; phi(3) = (e.eta30 - 3*e.eta12)^2 + (3*e.eta21 - e.eta03)^2; phi(4) = (e.eta30 + e.eta12)^2 + (e.eta21 + e.eta03)^2; phi(5) = (e.eta30 - 3*e.eta12) * (e.eta30 + e.eta12) * ... ( (e.eta30 + e.eta12)^2 - 3*(e.eta21 + e.eta03)^2 ) + ... (3*e.eta21 - e.eta03) * (e.eta21 + e.eta03) * ... ( 3*(e.eta30 + e.eta12)^2 - (e.eta21 + e.eta03)^2 ); phi(6) = (e.eta20 - e.eta02) * ( (e.eta30 + e.eta12)^2 - ... (e.eta21 + e.eta03)^2 ) + ... 4 * e.eta11 * (e.eta30 + e.eta12) * (e.eta21 + e.eta03); phi(7) = (3*e.eta21 - e.eta03) * (e.eta30 + e.eta12) * ... ( (e.eta30 + e.eta12)^2 - 3*(e.eta21 + e.eta03)^2 ) + ... (3*e.eta12 - e.eta30) * (e.eta21 + e.eta03) * ... ( 3*(e.eta30 + e.eta12)^2 - (e.eta21 + e.eta03)^2 ); disp('Object number:'); disp(i); Is = (m.m20 * m.m02-m.m11^2)/m.m00^4; Em1= 16*pi^2*Is Em2= (16*pi^2*Is)^-1 min(Em1, Em2) end
As I said in the comments, you need to use the central moments rather than raw moments as your code is doing. See Measuring Shape: Ellipticity, Rectangularity, and Triangularity Removing the unnecessary calculations for eta and phi, the last part of your code should be: % Calculate Raw Moments: m.m10 = sum(x .* f); m.m01 = sum(y .* f); m.m11 = sum(x .* y .* f); m.m20 = sum(x.^2 .* f); m.m02 = sum(y.^2 .* f); m.m30 = sum(x.^3 .* f); m.m03 = sum(y.^3 .* f); m.m12 = sum(x .* y.^2 .* f); m.m21 = sum(x.^2 .* y .* f); % DIPUM3E equations (13-28) through (13-30). xbar = m.m10 / m.m00; ybar = m.m01 / m.m00; % Calculate Central Moments: m.mu11 = m.m11 - xbar*m.m01; m.mu20 = m.m20 - xbar*m.m10; m.mu02 = m.m02 - ybar*m.m01; disp('Object number:'); disp(i); Is = (m.mu20 * m.mu02 - m.mu11^2)/m.m00^4; Em1= 16*pi^2*Is Em2= (16*pi^2*Is)^-1 min(Em1, Em2) With these changes, the results for the first two objects are: Object number: 1 Em1 = 1.000005232603003 Em2 = 9.999947674243773e-01 ans = 9.999947674243773e-01 Object number: 2 Em1 = 9.999818710527637e-01 Em2 = 1.000018129275901 ans = 9.999818710527637e-01
case statement in Dax
I have the following case when statement: case when ts.wgt_kg / ((hgt_cm / 100) * (hgt_cm / 100)) < 18.5 then 'Underweight < 18.5' when ts.wgt_kg / ((hgt_cm / 100) * (hgt_cm / 100)) between 18.5 and 24.9 then 'Normal 18.5-24.9' when ts.wgt_kg / ((hgt_cm / 100) * (hgt_cm / 100)) between 25.0 and 29.9 then 'Overweight 25-29.9' when ts.wgt_kg / ((hgt_cm / 100) * (hgt_cm / 100)) > 30.0 then 'Obese > 30.0' end as BMI How can i convert it into DAX? I tried to google it but I wasn't able to find anything useful. Can someone help me with that please. Thanks
Try something along these lines: BMI Category = VAR BMI = ts.wgt_kg / ( ( hgt_cm / 100 ) * ( hgt_cm / 100 ) ) RETURN SWITCH ( TRUE (), BMI < 18.5, "Underweight < 18.5", BMI < 25.0, "Normal 18.5-24.9", BMI < 30.0, "Overweight 25-29.9", "Obese > 30.0" ) This will return the first condition that evaluates to true or use the last argument if none of the above are true.
Multiplying constant to an expression
I am multiplying constant to an expression which can be seen from below. But the final expression is getting reduced. I just want it to be multiplied. (x^2 (-((1. (2 - 1/x)^3 x^5)/( Sqrt[1 - x^2] (0. + 1. x^2)^1.5)) + ((2 - 1/x)^3 x^5)/((1 - x^2)^( 3/2) (0. + 1. x^2)^0.5) + (3 (2 - 1/x)^2 x^2)/( Sqrt[1 - x^2] (0. + 1. x^2)^0.5) + (4 (2 - 1/x)^3 x^3)/( Sqrt[1 - x^2] (0. + 1. x^2)^0.5)))/(3 (2 - 1/x)^2) * (4) Kindly help. Thanks in advance.
It simplifies ok. expr = (x^2 (-((1. (2 - 1/x)^3 x^5)/ (Sqrt[1 - x^2] (0. + 1. x^2)^1.5)) + ((2 - 1/x)^3 x^5)/ ((1 - x^2)^(3/2) (0. + 1. x^2)^0.5) + (3 (2 - 1/x)^2 x^2)/ (Sqrt[1 - x^2] (0. + 1. x^2)^0.5) + (4 (2 - 1/x)^3 x^3)/ (Sqrt[1 - x^2] (0. + 1. x^2)^0.5)))/ (3 (2 - 1/x)^2)*(4); Simplify[expr] // InputForm (-2.6666666666666665*x^4*(-1.5*x^6*(x^2)^0.5 + 1.x^7(x^2)^0.5 + 1.5*x*(x^2)^1.5 - 4.5*(x^2)^2.5 + x^5*(-0.25*(x^2)^0.5 - 3.(x^2)^1.5) + x^4(1.375*(x^2)^0.5 + 3.(x^2)^1.5) + x^3(-0.75*(x^2)^0.5 + 3.25*(x^2)^1.5)))/ ((0.5 - 1.x)^2(x^2)^2.Sqrt[1 - x^2](-1. + x^2))
compute signal given (Signal + Noise) and Noise in dBm
Suppose we know (S + N) with x dBm and N with y dBm. Then S = 10 lg(10 ^ (x / 10) - 10 ^ (y / 10)) dBm. The problem is this computation necessitates float point, which is intensive on my embedded system. Is there any way to compute S (in dBm) more efficiently, preferably in integer only? Thanks in advance. p.s. S: signal N: noise
Can you afford two 1D lookup tables? Write 10 lg(10 ^ (x / 10) - 10 ^ (y / 10)) = 10 lg(10 ^ (x / 10)) # lookup by x + 10 lg(1 - 10 ^ ((y - x) / 10)) # by y - x
Function to return 3^k in n+1 calls
Can someone hep me find an algorithm for a recursive function func(int k) to return 3^k in only n+1 calls where k is in the range [ 3^n, 3^(n+1) ) For example, the function should return 3^1 or 3^2 in 1 call, 3^3, 3^4, .., 3^8 in 2 calls, 3^9, 3^10 .. in 3 calls and so on.
Here is the algorithm in untested C/C++: int 3pow(x) { switch(x) { case 1: return 3; case 2: return 9; case 3: return 27; } int remain = x % 3, recur = 3pow((x-remain)/3), combine = recur * recur * recur; switch (remain) { case 0: return combine; case 1: return combine * 3; default: return combine * 9; } } (I have not compiled, run, or otherwise tested this code. There may be syntax errors and other bugs. But it's sufficient to get the point across.) What's different about this algorithm is that it recurses by dividing by 3 instead of 2. This function is rather ugly by necessity, as abstracting away some of the pattern would likely involve more recursion. Still, this algorithm is now O(log3n). Here's a table of a given x from 1-50 and the number of recursive calls required (as a two-element lisp list): (1 1) (2 1) (3 1) (4 2) (5 2) (6 2) (7 2) (8 2) (9 2) (10 2) (11 2) (12 3) (13 3) (14 3) (15 3) (16 3) (17 3) (18 3) (19 3) (20 3) (21 3) (22 3) (23 3) (24 3) (25 3) (26 3) (27 3) (28 3) (29 3) (30 3) (31 3) (32 3) (33 3) (34 3) (35 3) (36 4) (37 4) (38 4) (39 4) (40 4) (41 4) (42 4) (43 4) (44 4) (45 4) (46 4) (47 4) (48 4) (49 4) (50 4) (51 4) (52 4) (53 4) (54 4) (55 4) (56 4) (57 4) (58 4) (59 4) (60 4) (61 4) (62 4) (63 4) (64 4) (65 4) (66 4) (67 4) (68 4) (69 4) (70 4) (71 4) (72 4) (73 4) (74 4) (75 4) (76 4) (77 4) (78 4) (79 4) (80 4) (81 4) (82 4) (83 4) (84 4) (85 4) (86 4) (87 4) (88 4) (89 4) (90 4) (91 4) (92 4) (93 4) (94 4) (95 4) (96 4) (97 4) (98 4) (99 4) (100 4) You can see that even as we get up over 100 it only take 4 recursive calls.