I am interested in using the function here:
http://uk.mathworks.com/help/nnet/ref/removerows.html
However, when I try to use it in Matlab it says: "Undefined function or variable 'removerows'"
I typed: exist removerows and returned a value of 0, suggesting that it's been removed. Has this function just been renamed? or is it part of a toolbox I may not have, the information does not detail this.
Much appreciated
According to the link that you posted, this function is part of the Neural Network Toolbox. So my guess is that you don't have this toolbox installed.
You can remove rows in a matrix by assigning an empty array to them.
This way you don't have to use functions belonging to toolboxes that require extra licences.
Example
A = [1 2; 3 4; 5 6]
A =
1 2
3 4
5 6
A(2,:) = [] %remove row 2
A =
1 2
5 6
Similarly you can provide an index array with the rows to be deleted in case you want to remove several ones.
Related
I tried the following code
data = [27 9 0
11.6723281 28.93422177 0
25 9 0
23 8 0
5.896096039 23.97745722 1
21 6 0
21.16823369 5.292058423 0
4.242640687 13.43502884 1
22 6 0];
Attributes = data(:,1:2);
Classes = data(:,3);
train = [1 3 4 5 6 7];
test = [2 8 9];
%%# Train
SVMModel = fitcsvm(Classes(train),Attributes(train,:))
classOrder = SVMModel.ClassNames
sv = SVMModel.SupportVectors;
figure
gscatter(train(:,1),train(:,2),Classes)
hold on
plot(train(:,1),train(:,2),'ko','MarkerSize',10)
legend('good','bad','Support Vector')
hold off
I tried both predict and svmclassify; but it returns an error. What is the basic difference between these two functions?
[label,score] = predict(SVMModel,test);
label = svmclassify(SVMModel, test);
First off, there's quite a big note on top of the documentation page on svmclassify:
svmclassify will be removed in a future release. See fitcsvm, ClassificationSVM, and CompactClassificationSVM instead.
MATLAB is a bit vague in its naming of functions, as there's loads of functions named predict, using different schemes and algorithms. I suspect you'll want to use the one for SVMs. This should return the same result as svmclassify, but I think that either something went wrong in determining which predict MATLAB decided to use, or that predict has a newer algorithm than the unsupported svmclassify, hence a different output may result.
The conclusion is that you should use the newest functions to be able to run your code in future releases and get the newest algorithms. MATLAB will choose the correct version of predict based on what kind of input structure you feed it.
I'm sure this is a trivial question for a signals person. I need to find the function in Matlab that outputs averaging of contiguous segments of windowsize= l of a vector, e.g.
origSignal: [1 2 3 4 5 6 7 8 9];
windowSize = 3;
output = [2 5 8]; % i.e. [(1+2+3)/3 (4+5+6)/3 (7+8+9)/3]
EDIT: Neither one of the options presented in How can I (efficiently) compute a moving average of a vector? seems to work because I need that the window of size 3 slides, and doesnt include any of the previous elements... Maybe I'm missing it. Take a look at my example...
Thanks!
If the size of the original data is always a multiple of widowsize:
mean(reshape(origSignal,windowSize,[]));
Else, in one line:
mean(reshape(origSignal(1:end-mod(length(origSignal),windowSize)),windowSize,[]))
This is the same as before, but the signal is only taken to the end minus the extra values less than windowsize.
Let's assume I have an array, array1, as follows:
array1 = [1 2 2 2 3 3 4 4 4 5];
I'm trying to find the first index value of each of the unique values in this array. This is pretty easy to do with the unique function:
[~,uniqueIndex,~] = unique(array1,'first')
which produces the expected output:
uniqueIndex =
1 2 5 7 10
I need to perform this action on thousands of very large arrays and the unique function has proven itself to be a huge bottleneck in my code. I was wondering if there are any alternatives that I could use to achieve the same end result (ie. the uniqueIndex variable), but with a smaller performance impact.
EDIT: The arrays are sorted and contain integers only.
Since we know that the arrays are already sorted, we can skip some of the checks that the MATLAB unique function carries out. The method that MATLAB uses internally is as follows (minus the checks)
n = 1:numel(array1);
d = [true,diff(array1)~=0];
uniqueIndex = n(d);
This should speed up your code at least a little.
I have a array X=[1 2 3 1.01 2.01 4 5 1.01 3.01] I want to all index of this array are similar and difference<=0.01 in matlab answer is
X1=[1 4 8], X2=[2 5],X3=[3 9],X4=[6],X5=[7]
many thanks
Here a solution using for-loop. Not sure about efficiency though. Gonna try to find another solution as well.
X=[1 2 3 1.01 2.01 4 5 1.01 3.01]
result=cell(length(X),1);
boarder = 0.01;
for n=1:length(X)
helper = X(n);
Y=X;
Y(X>helper+boarder)=0;
Y(X<helper-boarder)=0;
result(n,1)={find(Y)};
end
I predefine a cellarray (result) which contains all index (for each element of X). Then I loop over the elements setting those who are outside of your boarder to 0. Last but not least I save the index to the result array.
Obviously some results are the same but this way you get also results for the case: X=[ 1.01, 1.02, 1.03, 1.04,...];
And if you want to delete those elements which are the same you could loop over your data again and get unique results.
Y=cell(5,1)
for idx=1:numel(Y)
Y{idx}=find(abs(X-idx)<=.2);
end
I think the submission "unique with tolerance" in the FileExchange is for you.
You should note, that creating the variables X1...X5 as separate variables is a bad idea and a bad practice, because this makes referencing these values in later code (which is either vectorized or loop-based) cumbersome and inefficient. More correct alternatives to storing the data are cells (like in the solution suggested by Daniel) or in structs.
Having said that, if for some reason you still want to create uniquely named variables, this is possible using a mix of the aforementioned submission (uniquetol) and eval:
[~,b,c]=uniquetol(X,0.01+eps);
for ind1 = 1:length(b)
eval(sprintf('X%d = (find(c==X(b(%d))))'';',ind1,ind1));
end
I am trying to copy a few elements from a matrix, but not a whole row, and not a single element.
For example, in the following matrix:
a = 1 2
3 4
5 6
7 8
9 0
How would I copy out just the following data?
b = 1
3
5
i.e. rows 1:3 in column 1 only... I know that you can remove an entire column like this:
b = a(:,1)
... and I appreciate that could just do this and then dump the last two rows, but I'd like to use more streamlined code as I am running a very resource-intensive solution.
Elements in a matrix in MATLAB are stored in column-major order. Which means, you could even use a single index and say:
b = a(1:3);
Since the first 3 elements ARE 1,3,5. Similarly, a(6) is 2, a(7) is 4 etc. Look at the sub2ind method to understand more:
http://www.mathworks.com/help/techdoc/ref/sub2ind.html
You are not "removing" the second column, you are referencing the other column.
You should read some of the Matlab docs, they provide some help about the syntax for accessing portions of matrices:
http://www.mathworks.com/help/techdoc/learn_matlab/f2-12841.html#f2-428