passing data to torch.class(es) in lua - class

I'm trying to use the class function in Torch. however when calling what would be a member function with an argument, the argument is always nil. I know that class is an abstraction to emulate OO programming.
local RpnData, parent = torch.class('nn.RpnData', 'nn.Module')
function RpnData:__init()
parent.__init(self)
local scale = {8, 16, 32}
self._feat_stride = 1
self._allowed_border = 0;
end
function RpnData:Foo(input)
print("This will not work")
print(input)
end
local rpnnode = nn.RpnData()
local input = torch.Tensor( 5, 5):zero()
print(input)
rpnnode.Foo(input)
outputs:
Torch 7.0 Copyright (C) 2001-2011 Idiap, NEC Labs, NYU
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
[torch.DoubleTensor of size 5x5]
This will not work
nil
Can anyone suggest where i've gone wrong? Are there limitations to the Torch.class system?
Thanks in advance

Use rpnnode.Foo(rpnnode, input) or (better) rpnnode:Foo(input) - the colon is syntactic sugar, see Programming in Lua, 16.1.

Related

How to solve an inconsistent system?

I'm new on MATLAB and I want to use it to solve an Ax = b system. I did this on paper and know I want to know if it's right. The problem is that it is an inconsistent system.
A=sym([3/sqrt(29) 3/sqrt(29) -1 0 0 0;
1 -1 0 0 0 0;
4/sqrt(29) 4/sqrt(29) 0 0 0 0;
0 0 1 9/sqrt(101) 0 0;
0 0 0 2/sqrt(101) -1/sqrt(5) 1/sqrt(5);
0 0 0 4/sqrt(101) 2/sqrt(5) 2/sqrt(5)])
c=sym([0 0 -a 0 0 -a])
When I tried finding the solution with:
A/c
I get:
Warning: The system is inconsistent. Solution does not exist.
I found many themes about that on the Internet, but there was no solution. Does this mean that means MATLAB can't handle it or is there a way to get a solution?
The system is unfortunately not being solved properly. You need to use the ldivide (\) operator, not rdivide (/). Doing A/c is equivalent to A*c^{-1} and that's not what you want. To solve for the solution to the system, you must do A^{-1}*c or equivalently A\c. Also, in order to ensure that you get a proper solution, c needs to be a column vector, not a row vector. I'm also assuming that a is a constant which isn't declared in the current code.
Therefore:
syms a; %// Added
A=sym([3/sqrt(29) 3/sqrt(29) -1 0 0 0;
1 -1 0 0 0 0;
4/sqrt(29) 4/sqrt(29) 0 0 0 0;
0 0 1 9/sqrt(101) 0 0;
0 0 0 2/sqrt(101) -1/sqrt(5) 1/sqrt(5);
0 0 0 4/sqrt(101) 2/sqrt(5) 2/sqrt(5)]);
c=sym([0 0 -a 0 0 -a]).'; %// Change
out = A\c;
I get:
out =
-(29^(1/2)*a)/8
-(29^(1/2)*a)/8
-(3*a)/4
(101^(1/2)*a)/12
-(5^(1/2)*a)/4
-(5*5^(1/2)*a)/12

Create Vector Ranges From Variables

I have two vectors that hold the "start" and "end" locations (as logicals) that I wish to combine as to create a third vector, Final:
Starts = [0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0];
Ends = [0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0];
With the Final Vector looking like this:
Final = [0 0 0 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 0];
Currently, I can accomplish this using a for loop as follows:
Start_Locations = find(Starts);
End_Locations = find(Ends);
Final = zeros(20,1);
for x=1:length(Start_Locations)
Final(Start_Locations(x):End_Locations(x),1) = 1;
end
I was wondering if there's a way to accomplish the same exact thing without a for loop. For example, I could accomplish what I outlined above with the following "hard-coded" statement:
Final([4:8,11:19],1) = 1;
Specifically, is there a way to combine the Start_Locations and End_Locations vectors in a way such that I could have a single statement like:
Final(Combined_Start_and_End_Locations,1) = 1;
to accomplish what I did with the for loop above? I'm trying to learn to avoid for loops as much as I can and would really appreciate any solution that creates the Final vector as described above without resorting to a loop.
Problems like this can often be solved using either diff or cumsum. They are essentially discrete derivative and integration functions.
For your problem I believe that
Final = cumsum([Starts 0]-[0 Ends]);
Final = Final(1:end-1);
achieves the desired result.

is Matlab (R2009b) ignoring the transpose operator in "mldivide"?

I am trying to solve the linear system of equations A'*x = B using Matlab's "mldivide" (the backslash operator) in the form:
x_transp = A'\b;
A is a square sparse matrix and that is all I know about it.
The problem is that the transpose has no effect at all, so the result of the previous line of code is the same than:
x = A\b;
So, x = x_transp. However, either if I use a new variable such that:
A_transp = A';
x_transpOK1 = A_transp\b;
or simply use:
x_transpOK2 = transp(A)\b;
the result is different (x_transpOK1 = x_transpOK2 ≠ x = x_trans).
This behavior occurs in Matlab version 7.9.0 (R2009b) but it does not happen in 7.12 (R2011a).
This, however, does not happen with silly examples I have tried (the behavior then is correct). The matrices that make this behavior arise are:
A =[0.01 -0.495 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 -0.495 0 0 1 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 1];
b = [8
4
0
0
0
0
0
0
0
0];
Is it some kind of precision issue? Am I making any fundamental error I cannot see?
The guys at Mathworks replied: it is a bug in the interpreter, which have been fixed in the next versions. There is no fix for 7.9.0 and they recommend the following workaround:
A_transp = A';
x = A_transp\b;
I guess this is a great example of the typical advice to always be up-to-date...
My original post on Matlab Answers
The bug report
After all the discussion, here is my answer:
#Mario_Exec.bat, it seems to me that you might want to take this to the Matlab Answers (mathworks.com/matlabcentral/answers) as maybe someone with knowledge of the actual code (ie a Matlab employee) might be able to help you more specifically. It is an interesting question but it seems there is more going on that might need more knowledge of the actual code and decision trees.
Please do post back here when you hear back. I am curious what they say!

Matlab going from text to struct

I have an m-file with a big (working) structure. After lots of effort I've found a way to update some of structs with fprint etc. I got the file saved and everything seems good when it´s saved but I want to use the struct further down in the function.
I can run the m-file (that stores the struct) by
run(FileName)
Actor=ans;
But it doesn't work all the time and seems like a bad way of doing it.. If i write:
Actor=IndataActor %The name of the indatafile is IndataActor..
It works fine (Actor got the struct)
But I want to use the variable "FileName"
Actor=FileName
Actor just got the name of the FileName (Actor='IndataActor')
Any suggestions?
Well the struct is saved in an m-file (as a function)...
function [ Actor ] = IndataActorsLund3
%Actor 1
Actor{1}.Name='Räddningstjänsten Syd';
Actor{1}.ExpertNames={'Saknas?'};
Actor{1}.Units={'Saknas?'};
Actor{1}.Titles={'Saknas?'};
Actor{1}.NbrGoals=5;
Actor{1}.Goals={'Trygghet för medborgare', 'Bränder och andra olyckor ska minska','Öka kunskapen angående olyckshantering och riskmedvetenheten', 'Påbörja insats inom 10 minture i 90% av prioriterade olyckor', 'Bryta negativ trend vid insats inom 15min'};
Actor{1}.NbrActivities=6;
Actor{1}.Activities={'Tillsyn, remishantering','Informationsinsatser', 'Internutbildning', 'Externutbildning', 'Skadeavhjälpande insats', 'Olycksutredning','','','',''};
Actor{1}.MatrixActGoals=[...
3 4 3 4 3;
5 3 4 2 2;
3 3 1 4 5;
4 3 4 2 2;
5 1 1 5 5;
3 3 2 3 3];
Actor{1}.NbrInfluencingFlows=1;
Actor{1}.InfFlowType(1)=19;
Actor{1}.InfFlowMatrix{1}=[...
0 0 0 0 0 0 0;
0 0 0 0 0 0 0;
0 0 0 0 0 0 0;
0 0 0 0 0.4 0.6 1;
0 0 0 0 0.2 0.5 1;
0 0 0 0 0 0 0];
Actor{1}.NbrDependentFlows=4;
Actor{1}.DepFlowType(1)=1;
Actor{1}.DepFlowMatrix{1}=[...
0 0 0 0 0 0 0;
0 0 0 0 0 0 0;
0 0 0 0 0 0 0;
0 0 0 0.1 0.3 0.5 0.7;
0 0 0 0.6 0.8 0.9 1;
0 0 0 0 0 0 0];
.... (and about 1000 more rows)
end
Than I have created a GUI (with GUIDE) to add and change data within the struct. So I read in the file and do changes in the file with several different fprint commands. And struct gets saved in the m-file as well that is already taken care of. Now I don´t know the best way to assign the struct to a variable.

Applying Threshold mask

i am making image compression in matlab.
After i applied DCT on image and i had img matrix, i want to apply a threshold mask on that matrix.
mask = [1 1 1 1 0 0 0 0
1 1 1 0 0 0 0 0
1 1 0 0 0 0 0 0
1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0];
maskedImg = blkproc(img,[8 8],mask );
I used that function but it didnt work and i get error message:
Error in ==> blkproc at 67
[a, block, border, fun, params, padval] = parse_inputs(varargin{:});
According to latest Matlab documentation; closest blockproc syntax (for your case) is B = blockproc(A,[M N],fun). So apparently your mask really should be a function!
However, I recall that blkproc has been a valid Matlab function for a while ago, thus double check the proper way to call it by typing (in the command line) > help blkproc. (Al tough I'm quite confident that it will share the calling signature with blockproc [in this case]).