Eigen equivalent of find() in Matlab - find

I'm trying to write my code from Matlab to c++ using Eigen, I have a boolean matrix
A << 1 0 0
0 1 0
1 1 0
I'm using find function in Matlab to get the indices of non-zero values find (A) --> 1 3 5 6, how to do the same think with Eigen ? Thanks.

As of today that is not implemented inside Eigen. So essentially, you need to loop through your matrix manually. Something like this should work (Eigen will start indexing at 0, of course):
Eigen::Matrix<bool, ....> A;
std::vector<Eigen::Index> idxs;
for(Eigen::Index i=0; i<A.size(); ++i)
if(A(i))
idxs.push_back(i);

I'd like to you should see libigl. libigl is a simple C++ geometry processing library. It has a wide functionality for matrix manipulation which make Eigen feel a lot more like MATLAB.
And you can check this website to see matlab to eigen.
As a suggestion from #ChristianB, i added an image to show matlab to eigen.

Related

Trying to create matrix when error Attempted to access MYAWT1(10); index out of bounds because numel(MYAWT1)=9. in Matlab

i'm a beginner user of matlab(i would be very gratefu if you explain the solution in a simple language, consider me a 5 years old). What im trying to do is create a 21620x1 rotation matrix of a ship's motion. Since the amount of data is too many to do it manually, im trying to use the command for. Here is my code
MROLLT1=zeros(3,3,3)
MPITCHT1=zeros(3,3,3)
MYAWT1=zeros(3,3,3)
MR=zeros(3,3,3)
for n=1:21620
MROLLT1=[1 0 0;0 cosd(RollT1(n)) -sind(RollT1(n));0 sind(RollT1(n)) cosd(RollT1(n))]
end
for n=1:21620
MPITCHT1=[cosd(PitchT1(n)) 0 sind(PitchT1(n));0 1 0;-sind(PitchT1(n)) 0 cosd(PitchT1(n))]
end
for n=1:21620
MYAWT1=[cosd(YawT1(n)) -sind(YawT1(n)) 0;sind(YawT1(n)) cosd(YawT1(n)) 0;0 0 1]
end
for n=1:21620
MR=MYAWT1(n)*MPITCHT1(n)*MROLLT1(n)
end
When i run the program, there is an error like this
Attempted to access MYAWT1(10); index out of bounds because numel(MYAWT1)=9.
Error in Ship_Reference_Frame_Integration_R1 (line 40)
MR=MYAWT1(n)*MPITCHT1(n)*MROLLT1(n)
Could you please explain to me how to create rotation matrix(MR) for every data from data 1 to data 21620?
And by the way, a source in another post told me that i should create matrix with command 'zeros'(as in the above code) to define the matrix. What does it mean?

Matlab matrix command issue

How can this MATLAB command work assuming that I is an image and Y a 1*256 vector?
img=round(y(I));
Thank you for your attention
The values in the matrix of the image (created from using I=imread('./imagefile.png') or whatever) are apparently within the range 0 to 255 (or maybe you shifted them from 1 to 256?) and they are being used as the value for the index into y. E.g., you get a matrix returned that looks like
round(y(I(1,1))) round(y(I(1,2)) round(y(I(1,3)) ...;
round(y(I(2,1))) round(y(I(2,2)) ...
...

Zero padding in Matlab

I have image, and I want to do up sampling. First of all I need to plug zeros between pixels such that [1,2,3] transforms to [1,0,2,0,3]. Can anyone tell me how to do it without using paddarray and without using for loops?
Thank you in advance!
Something like this?:
B=zeros(size(img)*2);
B(1:2:end,1:2:end)=img;
However there are ways of up-sampling in matlab without having the need of doing it by hand, for example interp2
You could also make use of MATLAB's way of dynamically allocating variables if you don't specify a number for an index into the array. By omitting indexing into certain locations in your array, MATLAB will fill in these values with zeroes by default. As such:
B(1:2:5) = 1:3
B =
1 0 2 0 3
V = [1,2,3];
padded(numel(V)*2) = 0;
padded(1:2:end) = V
And then just deal with the trailing zero if numel(V) was odd
There is a function upsample that does exactly this from Octave-Forge -- see docs on upsample.
Or you can look at the source of upsample to see what implements it. Are you opposed to using a package or a function?

How to dump variables as MATLAB source code?

Is there a way to dump a MATLAB variable as the source code for the corresponding literal initializer? IOW, I'm looking for some function x such that, for example:
>> A = zeros(2);
>> x(A)
ans =
[0 0; 0 0]
>> class(x(A))
ans =
char
Is there such a function, or an easy way to achieve the same effect? (I realize that literal initializers may not exist for some MATLAB items; for such items the problem is intrinsically unsolvable.)
I am aware of the fact that MATLAB offers many ways to save data to files, but none of the ways I've found produce MATLAB source code, which is what I'm after.
For simple numeric values (and also char arrays), the mat2str function does what you're looking for.
For example, (from the MATLAB documentation):
Consider the matrix
x = [3.85 2.91; 7.74 8.99]
x =
3.8500 2.9100
7.7400 8.9900
The statement
A = mat2str(x)
produces
A =
[3.85 2.91;7.74 8.99]
where A is a string of 21 characters, including the square brackets, spaces, and a semicolon.
Further, passing the string 'class' as the second argument ensure that the answer will be case to the correct numeric type.
See the MATLAB documentation for mat2str, or run
doc mat2str
in MATLAB, for more information.
I know you are looking for a function that can do this, rather than an interactive procedure, but for anyone else who wants to do this manually...
The MATLAB variable editor/viewer has built-in code generation functionality. Open the variable in the editor, click the save icon, and choose MATLAB Script (*.m) file type (default is .mat):
The resulting MatrixCode.m:
% -------------------------------------------------------------------
% Generated by MATLAB on 3-Mar-2014 17:35:49
% MATLAB version: 8.3.0.73043 (R2014a)
% -------------------------------------------------------------------
M = ...
[16 2 3 13;
5 11 10 8;
9 7 6 12;
4 14 15 1];
Maybe someone with Java and reverse engineering skills can figure out how to call this GUI operation from the command line.
As Sam Roberts commented, matlab.io.saveVariablesToScript is now the ultimate method to convert any datatype to a script. This method was introduced in 2014a and works for struct, cell, and all primitive datatypes.
chappjc's method is also correct, but it uses a MATLAB GUI frontend to the saveVariablesToScript method.

Use logical indexing instead of FIND

Within a loop in my code I use a one-liner to find and plot the minimum of some potential (for clarity: the 7 corresponds to the cells containing the potential values, the 5 to the x-values):
plot(PDdata{subject,5}{1,1}(find(PDdata{subject,7}==...
min(PDdata{subject,7}))),min(PDdata{subject,7}),'ko')
Now Matlab suggests to use logical indexing instead of FIND and although I've looked into it only briefly, it doesn't strike me as something I should do here. Main question here is thus whether I should employ logical indexing (I prefer to keep it a one-liner!), and if so: how?
I apologize in advance for asking such a minor question, but I'm trying to increase my Matlab knowledge so hopefully a short answer would help me out already!
Dennis is correct in the comment. The idea is that using logical indexing directly cuts out a step. So if you're trying to extract all the elements in a matrix that are greater than 2 for example, using find you would do this:
A = [1 3 2 1 4 1]
A(find(A>2))
which becomes something like
A(find([0 1 0 0 1 0]))
then
A([2, 5])
and finally
[3, 4]
However if you used logical indexing directly like this:
A(A>2)
You get
A([0 0 1 0 0 1 0])
and finally
[3,4]
So you get exactly the same result, and you skip a call to find which as you can see is completely extraneous in these cases.
Then just to add something pretty cool, unless your Matlab is pretty old, the mlint (the bit that gives you that warning) can actually fix this for you. If you hover over the find which is underlined in red you get this:
So this is a basic version of exactly that mistake, see at the end there is a little fix button. This is what you get after clicking it:
OK so in this instance it's normal indexing instead of logical but the point remains, mlint can fix this for you which is pretty awesome!