Unknown flag error diffset 'stable' - matlab

I am trying to use diffset, with the set being stable, but i get a Unknown flag. error. I also tried it with sorted, but the same error applies. Is there a different syntax?
My code:
C = setdiff([4 1 3 2],[2 1],'sorted')
I am using Matlab 7.10.0 r2010a

As mentioned, this is probably a MATLAB version issue. The behavior of setdiff has changed in the last versions, and the online documentation covers only MATLAB's latest 2013a release, so this may be a possible reason for your confusion.
That said, the documentation for previous MATLAB releases is also available online (note that it requires a MathWorks account, though). In any case, your MATLAB seems to have the older implementation of the setdiff command, and you can verify that by:
help setdiff
Anyway, the behavior of the older implementation of setdiff is similar to the behavior of the newer implementation when the flag setOrder is set to 'sorted'. If you want to mimic the behavior when the flag setOrder is set to 'stable', you can use ismember instead, for example:
A = [4 1 3 2];
B = [2 1];
C = A(~ismember(A, B))
which yields:
C =
4 3

This post offers an implementation:
function [res]=setdiff_stable(a,b)
if(size(a,1)>size(a,2))
a=a';
end
if(size(b,1)>size(b,2))
b=b';
end
res=a(sum(repmat(a,length(b),1)-repmat(b',1,length(a))==0,1)==0);
end

Related

Why MATLAB returns an error using `conv` function?

I'm using MATLAB 2015a. Even if I try to run document example of conv function, i get an error saying Error using conv (line 15), Not enough input arguments..
This is the example code I'm using:
u = [1 0 1];
v = [2 7];
w = conv(u,v)
What is the problem with my MATLAB?
It is hard to find the documentation for that version online without a license. You can find the documentation for your version by typing
help conv
Presumably the interface changed after your version. So you have to see what your documentation says.
FWIW, the documentation archives are here, but I cannot access them.
Also, I tried your code in Matlab 2015b (that is, b, not a), and it worked. So it must have changed between those two versions.
According to excaza, the docs haven't changed. So, like they say, it must be a shadowing issue. You would verify that by using clear all before your code snippet.

Cannot use name-value pair 'ForceCellOutput'-cellOutput(logical) in strfind command in Matlab R2015a; when was this addition introduced?

I was just about to find use for the ..., 'ForceCellOutput', true) name-value pair argument for the strfind command, as is described in the reference docs for strfind:
Syntax
k = strfind(str,pattern)
k = strfind(str,pattern,'ForceCellOutput',cellOutput))
...
Return Indices in Cell Array
Find the occurrences of a pattern in a character vector. Force
strfind to return the indices of those occurrences in a cell array.
Then display the indices.
Create a character vector and find the occurrences of the pattern ain.
str = 'The rain in Spain.';
k = strfind(str,'ain','ForceCellOutput',true)
The above does, however, yields an error for me (Error using strfind: Too many input arguments), and I noticed (after the error occurred) that the 2nd syntax above is not included in the native Matlab help, help strfind, for the version I'm running, R2015a.
I can't find any mention of this update, however, in the release notes for R2015b or R2016a.
Could someone verify that this optional syntax works for ver. > 2015a?
Is it common that minor (expanding/non-breaking) updates to Matlab commands are not included in the release notes? (Or have I've simply missed any mention regarding this one?)
The sample code functions properly in R2015b but is not present in the documentation (requires login). It is, however, included in the inline documentation for strfind:
% IND = STRFIND(TEXT,PATTERN,'ForceCellOutput',CELLOUTPUT) forces IND
% to be a cell array when CELLOUTPUT is true.
So it looks like it was an undocumented addition in R2015b. With the recent release of R2016a, the online documentation is now for R2016a, so they have documented the change moving forward.

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.

How to force Matlab/Octave cov function to use optional parameter

I'm using the cov (covariance) function of Matlab and Octave. Actually I'm using Octave, but in the end it has to work for both. This function has an optional second or third parameters to indicate whether normalization should be done with N or N-1.
If I do this: cov(points,1) (where points is 4x2 matrix) I get following error:
error: cov: x and y must have the same number of observations
On a general note I would like to know how is Matlab/Octave able to distinguish if the second parameter is another matrix or an optional parameter (because it can have 2 or 3 parameters).
More specifically I would like to know how can I solve my problem?
Matlab cov() documentation: http://www.mathworks.de/help/techdoc/ref/cov.html
Octave cov() documentation: http://www.gnu.org/software/octave/doc/interpreter/Correlation-and-Regression-Analysis.html
EDIT: I'm using Octave 3.2.4 on Ubuntu 12.04
EDIT2: The solution is to install a newer version of Octave. This features was implemented after 3.2.
This looks like an Octave bug (at least with version 3.0.5); it works fine in Matlab (at least with version 7.10).
Of course, to work around, you could just compute:
cov(a) * (N-1) / N
type help cov, and note that opt has been removed doesn't exist.
better yet, do
gedit /usr/share/octave/3.2.4/m/statistics/base/cov.m
and compare it to the old new code
Opt is gone recent. Use Oli's trick in the meantime
Edit: I take that back, this is a new feature. Not sure which version of Octave is using it, but you need an upgrade. Either, you do an unstable synaptic upgrade if you can find a repository, or you build it from source.
Edit 2: feature added Jan 2011 (3.4.1). 3.2.4 released Jan 2010. current version is 3.6.1

bi2de error in MatLab

In MatLab, "> help bi2de" provides the following example:
B = [0 0 1 1; 1 0 1 0];
D = bi2de(B)
But when I try this on my own, I get the following error:
??? Undefined function or method 'bi2de' for input arguments of type 'double'.
Is there something wrong with this function in MatLab?
I am pretty sure that the reason why this problem happened is because of the license of the toolbox, Communications system toolbook, in which this function belongs in. Write which bi2de and see what will be the result. If it returns path of the function and the comment Has no license available, then the problem is related to the license. That means, license of the toolbox is not set correctly. Mostly it happens if the toolbox is added later, i.e., after installation of the original matlab. Please check and solve the license issue, then it will work fine.
bi2de is a function in the Communications toolbox. You need to have that toolbox to use it. If you do have that toolbox, then the problem is that your B matrix is being treated as double instead of binary (I don't have the toolbox so I can't test this).
Consider using bin2dec, which turns a string representation ('1011001', eg) into a decimal number. This function is not part of a toolbox; it's available as part of the basic MATLAB package.