Matlab string array - matlab

All i am trying to do is this:
type = cell(size(A));
...
i = find(A == 0);
type{i} = 'pasok';
However it miserably fails is size(A) > 1 or if i is empty.
Is there any workaround for this problem?
UPDATE -ERROR
type =
[] []
ans =
1 2
i =
1 2
The right hand side of this assignment has too few values to satisfy
the left hand side.
Error in ellipse (line 48)
type{i} ='pasok';

To assign one value to multiple cell-entries at once, you can use
[type{i}] = deal('pasok');
Note that type{i} has to be in square brackets.

Related

Undefined operator '.^' for input arguments of type 'dataset' - MatLab

I have this matlab function to calculate the cosine similarity between 60 vectors of a dataset. 60 rows correspond to the vector number and 2 columns to the x and y components of each vector.
function [cosSim] = cosineSimilarity(data)
[n_row n_col] = size(data);
norm_r = sqrt(sum(abs(data).^2,2));
for i = 1:n_row
for j = i:n_row
cosSim(i,j) = dot(data(i,:), data(j,:)) / (norm_r(i) * norm_r(j));
cosSim(j,i) = cosSim(i,j);
end
end
end
The main script:
cd(matlabroot)
cd('help/toolbox/stats/examples')
ds = dataset('XLSFile','TestCosSim.xlsx');
c = cosineSimilarity(ds);
When I run the script, I get the following errors:
Undefined operator '.^' for input arguments of type 'dataset'.
Error in cosineSimilarity (line 8)
norm_r = sqrt(sum(data.^2,2));
Error in CosSimTest (line 6)
c = cosineSimilarity(ds);
Does anybody have an idea of why this is happening?
Thanks a lot in advance.

Reshape function output size is unknown

I am using Matlab coder to compile some .m files into C static library. In the function below, I am getting the following errors:
function net = mlpunpak(net, w)
nin = net.nin;
nhidden = net.nhidden;
nout = net.nout;
mark1 = nin*nhidden;
net.w1 = reshape(w(1:mark1), nin, nhidden); % Error1 ***
mark2 = mark1 + nhidden;
net.b1 = reshape(w(mark1 + 1: mark2), 1, nhidden); % Error2 ***
mark3 = mark2 + nhidden*nout;
net.w2 = reshape(w(mark2 + 1: mark3), nhidden, nout);% Error3 ***
mark4 = mark3 + nout;
net.b2 = reshape(w(mark3 + 1: mark4), 1, nout); % Error4 ***
Error1: Dimension 1 is fixed on the left-hand side but varies on the
right ([10 x 8] ~= [:? x :?]). Error2: Dimension 1 is fixed on the
left-hand side but varies on the right ([8 x 1] ~= [:? x :?]).
Error3: Dimension 1 is fixed on the left-hand side but varies on the
right ([8 x 1] ~= [:? x :?]). Error4: Dimension 2 is fixed on the
left-hand side but varies on the right ([1 x 1] ~= [1 x :?]).
The value of the variables are nin=10, nhidden=8, nout=1 and this function overwrites the fileds of the net. Any help is highly appreciated.
I think you gave the fields w1, b1, w2, b2 the fixed dimensions somewhere. In this case, you are using a variable-size array as input of reshape, that causes the problem. Have a look at this.
UPDATE: ok, i think i solved the errors. In the Overview tab of Matlab coder, i tried to define the fields as matrix of double with unbounded dimensions. And whoops, Code generation successful: View report :-)
Btw, at the Error 2, i think it's your fault, since the output of reshape here should be 1x8, you have to check it yourself regarding to your algorithm.

classification with four classes by matlab

I have a classification problem with four classes of input vector.The four classes are
A = [1 , 1; 1 ,2];
B = [2,2; -1,0];
C = [-1,-2;2,1];
D = [-1,-2; -1,-2];
I wan Implement this problem by Matlab, I Use this code :
C = [-1,-2;2,1];
A = [1 , 1; 1 ,2];
B = [2,2; -1,0];
D = [-1,-2; -1,-2];
hold on
grid on
plot(A(1,:),A(2,:),'bs')
plot(B(1,:),B(2,:),'r+')
plot(C(1,:),C(2,:),'go')
plot(D(1,:),D(2,:),'m*')
a = [0 1]';
b = [1 1]';
c = [1 0]';
d = [0 0]';
P = [A B C D];
T = [repmat(a,1,length(A)) repmat(b,1,length(B)) repmat(c,1,length(C)) repmat(d,1,length(D)) ];
net = perceptron;
E = 1;
net.adaptParam.passes = 1;
linehandle = plotpc(net.IW{1},net.b{1});
n = 0;
while (sse(E))
n = n+1;
[net,Y,E] = adapt(net,P,T);
linehandle = plotpc(net.IW{1},net.b{1},linehandle);
drawnow;
end
but My Code does'nt work I have No idea why, please Help me....
As has been suggested by thewaywewalk, the trouble is your while-loop and the fact that you do not provide an adequate check for the statement you wish to evaluate.
Replace your while-statement with these two lines:
ACCEPTABLE_ERROR = 3.0;
while (sse(E)>ACCEPTABLE_ERROR)
And you should see your script terminate after three iterations. You can play with the ACCEPTABLE_ERROR variable to check which solution works best for you. If you set it too small, your while loop will not exit, because the statement will not be false.
An explanation to your original while-statement:
All you ever evaluated if sse(e) returned a results - which it did in each case. That's why it never stopped.
To the question of sse requires more than one input argument:
That depends on what input arguments you provide.
The documentation says:
perf = sse(net,t,y,ew) takes these input arguments and optional function parameters,
net: Neural network
t: Matrix or cell array of target vectors
y: Matrix or cell array of output vectors
ew: Error weights (default = {1})
and returns the sum squared error.
However, it is not necessary, to provide the error weights, ew, as the source code reveals:
Only the first three arguments are required. The default error weight is {1}, which weights the importance of all targets equally.
In your case you should, based on the documentation, call sse like this:
sse(net,T,Y)
Without being mentioned in the documentation (or I haven't found it), this is equivalent to what you have done, by providing simply the network errors, E as provided from adapt:
sse(E)
Both give the same results.

Matlab error: Index exceeds matrix dimensions

I'm trying to perform STFT on an audio file. I need to get the fft of each window.
I used the follwing code.
[wave,fs] = wavread('40.wav');
w_length = 1024;
for v = 1:w_length:length(wave)
data_sub = wave(v:v+w_length);
subsection_fft = fft(data_sub);
figure(1)
plot(subsection_fft)
end
But i get the following error.
??? Index exceeds matrix dimensions.
Error in ==> chk at 7
data_sub = wave(v:v+w_length);
Can you tell me what I can do to rectify this.
As the error message says, you are trying to access a position in wave tat does not exist.
See this example:
a = rand(7,1);
step = 4;
1:step:7
ans =
1 5
when v = 5, you will try to access position v:v+step, i.e. 5 to 9, but a is only defined up to 7 elements.
In your case, wave is defined up to length(wave), but on the last iteration you will go out of bounds.
To avoid it, on approach would be to sample the end sequences and subtract the length of the sequence:
pos = (1+w_length:w_length:length(wave))-w_length
for v = pos
% do stuff
end
However, you will be left with some unprocessed part which you will have to do outside of the loop as last iteration.

My fft2 function not work with me

I faced a problem when I convert these two statements from build in to my own function:
gaus=gauss/sum(gauss(:));
BeforeAbs=fft2(gaus,size(im,1),size(im,2));
And it gave me [Attempted to access X(2); index out of bounds because numel(X)=1]
%--------Start convert from build in to my own function of Fourier transformation of 1 D
gaus=gauss/sum(gauss(:));
for u=1:(gaus)
summ=0;
for x=1:1
w2=(-1*(sqrt(-1)))*2*pi*((u*x)/(gaus+1))
summ=summ+(gaus(x)*exp(w2));
end
PQ2(u)=summ;
end%
X=size(im,1);
for u=1:(X)
summ=0;
for x=1:1
w3=(-1*(sqrt(-1)))*2*pi*((u*x)/(X+1))
summ=summ+(X(x)*exp(w3));
end
PQ3(u)=summ;
end
BeforeAbs=[PQ2 PQ3];
Can anyone tell me why this error appears with me?
In your code, the statement
X=size(im,1);
returns the size of the variable im along its first dimension. That would return a scalar value. However, you have a for loop:
for x=1:2
...
summ=summ+(X(x)*exp(w3));
end
that iterates over the values x = 1 and x = 2. When you try to evaluate X(x) when x = 2, you get the error because X has only one element.
Basically, you are doing something equivalent to this:
X = 5
X(2)
Also, your for-loop for u:
for u=0:(X-1)
starts from u = 0, but later you try to set
PQ3(u)=summ;
However, array indexing in MATLAB is 1-based, so PQ3(0) would result in an error. You should start indexing from 1: PQ3(1) = summ.