syntax of Iteration in K-mean Clustering With MATLAB [closed] - matlab

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
i have a problem with using the number of iteration in MATLAB.
i use this code but there is no change in the number of iteration:
Edit
this is my code...
clc;clear;close all;diary temp;
cluster_data=unifrnd(-3,+3,[100 2]);
optns = statset('MaxIter',500);
[idx,ctrs]=kmeans(cluster_data,3,'dist','city', ...
'display','iter','replicate',100,'options',optns);
figure
plot(cluster_data(idx==1,1),cluster_data(idx==1,2),'r.','MarkerSize',14)
hold on
plot(cluster_data(idx==2,1),cluster_data(idx==2,2),'b.','MarkerSize',16)
plot(cluster_data(idx==3,1),cluster_data(idx==3,2),'g.','MarkerSize',18)
plot(ctrs(:,1),ctrs(:,2),'kx',...
'MarkerSize',12,'LineWidth',2)
plot(ctrs(:,1),ctrs(:,2),'ko',...
'MarkerSize',12,'LineWidth',2)
legend('Cluster 1','Cluster 2','Cluster 3','Centroids',...
'Location','NW')
fid=fopen('temp');
dat=textscan(fid,'%s');
fclose(fid);
delete temp
dat=dat{1};
i1=find(~cellfun('isempty',strfind(dat,'sum')));
ie=find(~cellfun('isempty',strfind(dat,'iterations')));
i1=i1(1)+1;
Nd=str2num(dat{ie(1)-1});
ie=Nd*4+i1-1;
dat=reshape(str2num(strvcat(dat{i1:ie})),4,Nd)';
iter = dat(:,1) % <-- iterations
sm = dat(:,4) % <-- sum
figure
plot(iter,sm)
the issue is the number of iteration... how can i really increase the number of iteration?
however i increase the 'Maxiter' but no change appear.

You should use statset to change the number of iterations:
optns = statset('MaxIter',500);
And the call kmeans like this:
[idx,ctrs]=kmeans(cluster_data,3,'dist','city', ...
'display','iter','replicate',100,'options',optns);
Comment
The documentation for kmeans states:
MaxIter - Maximum number of iterations allowed. The default is 100.
So if you change the value of MaxIter it should be a number different from 100.

Related

Dont understand the function of cmd_data(ii) = cell2mat(textscan(char(data{i}(ALL_STRT(ii):(ALL_STRT(ii)+4))),'%f')); at all [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
IND_STRT = 0;
ALL_STRT = IND_STRT:12:510;
cmd_data = zeros(length(ALL_STRT),1); %example: x=zeros(1,21) gives you a 1 by 21 matrix
for ii = 1:length(ALL_STRT) %declare variable ii to run from the row of length
if ~isempty(data{i})
cmd_data(ii) = cell2mat(textscan(char(data{i}(ALL_STRT(ii):(ALL_STRT(ii)+4))),'%f'));
end
end
I need to read the EPS from EnduroSat, however i have difficulty understanding the line cmd_data(ii) = cell2mat(textscan(char(data{i}(ALL_STRT(ii):(ALL_STRT(ii)+4))),'%f'));
Im required to utilised MatLab to code and this particular line have an error and i don't understand why.
Whenever you see a complicated line like this in MATLAB, try to break it up.
% find some indices. These values have been selected by the programmer/data, can't say why.
a=ALL_STRT(ii):(ALL_STRT(ii)+4)
% obtain that articular section of the data
b=data{i}(a)
% convert it to a char data type (characters)
c=char(b)
% scan text, and treat them as float
d=textscan(c,'%f')
% the output is a cell array, we want a matrix instead. Make the cell array into a matrix.
cmd_data(ii) = cell2mat(d)
You can read particularly what each of these do better in their documentation pages, and you can see it work if you put a break-point in the code, and see what each of this parts output when you call it. Learn how to debug, is a very very powerful tool

Why is my for loop running over? (Matlab) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have the following code:
for i = 1:RGB_size(2) % RGB_size(2) = 1296 //X coords
for j = 1:1:RGB_size(1) % RGB_size(1) = 964 //Y coords
if mask(i,j) == 1
data(next_pixel,:) = [ImgIndex, ImgTake, i, j, RGB(i,j,1), RGB(i,j,2), RGB(i,j,3),...
HSIR(i,j,1), HSIR(i,j,2), HSIR(i,j,3)];
next_pixel = next_pixel + 1; %get next pixel
end
end
end
But Matlab won't run my code because it says I'm trying to access mask(965,1) and my variable mask size is 1296×964. However, I don't see how this is possible. Any thoughts?
RGB_size is calculated from a variable called RGB, which is the same variable used to create the variable mask. I have verified that they are both the same size with the debugger.
Actual error message is:
Attempted to access mask(965,1); index out of bounds because size(mask)=[964,1296].
You have switched the i and j index of the mask.

Visually midway between two points on a x axis log scale [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
The follow plot shows my question:
I would like to add a line between the points of 1e-1 and 1e-2. So I thought just (1e-1+1e-2)/2.
But for a log scale that is not "midway".
How can I calculate the "visual" midway value between these, or any two points in this case? Code used is
clc; clear all;
y = logspace(-3,0,100);
x = y;
semilogx(y,x);
hold on
plot([1e-1 1e-1],get(gca,'YLim'),'k--');
plot([1e-2 1e-2],get(gca,'YLim'),'k--');
midway = (1e-1+1e-2)/2;
plot([midway midway],get(gca,'YLim'),'k--');
Thanks
a=1e-2
b=1e-1
midway = exp((log(a)+log(b))/2)
Take the log to get the positions in log scale, then do the math.
You could simplify that formula and you will end up with a geometric mean:
midway=sqrt(a*b)

Find Audio Peaks in MATLAB [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an audio signal of about the size 7000000 x 1. I have used the peakfinder m file in MATLAB to find the location of all of the peaks in the audio file above a specific threshold. I am now trying to find a frame sized 1000000 x 1 that contains the greatest amount of peaks. I am completely lost on how to do this and any help would be greatly appreciated. Thank you!
Well, all the peak finder function is doing is taking the second derivative and looking for any place where the resulting value is negative. This indicates a local maximum. So you can do something very similar to find any local maximum.
Once you have these indices, you can window the array containing a logical representation of the locations, and count how many peaks are there.
The code below will do what I am saying. It will window across and count the number of peaks found, and return a a vector of the counts, which you can then just find the max of, and then you have the starting index.
clc; close all; clear all;
A = randi(10,[1,100])
plot(A)
hold on
C = diff(diff(A))
indices = find(C < 0)+1;
scatter(indices,A(indices),'r')
temp = zeros(size(A));
temp(indices) = 1;
window = ones(1,5);
results = conv(temp,window,'same');
max(results)
This is of course a pet example, A would be your matrix, and window would be a matrix the length of the range you want to examine, in your case 1000000
Edit
As Try Hard has made note of in the comments below, this method will be fairly susceptible to noise, so what you can do first is run a smoothing filter over the signal before doing any derivatives, something like as follows.
filt = (1/filtLength) * ones(1,filtLength);
A = conv(A,filt,'same')
This is a simple averaging filter which will help smooth out some of the noise

How to apply sliding window for subtracting two different images in matlab? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to apply sliding window for subtracting two different images in matlab,
the window size must be 4X4,
please help me
i want to find similarity value between two different images.if A and B are two 2 images take difference between each 4x4 matrix of each A&B in sliding window manner
i tried a code ,i dont know whether it is correct or not
m=imread('index.jpeg');
sal=imread('salt.jpg');
salt=rgb2gray(sal);
ab=rgb2gray(m);
imshow(ab);
imh=size(ab,2);
imw=size(ab,1);
wh=4;
ww=4;
k=0;
disp(imh),disp(imw);
if 1
for j=1:imh+wh-1
for i=1:imw+ww-1
w1=ab(j:j+wh-1,i:i+wh-1,:);
w2=salt(j:j+wh-1,i:i+wh-1,:);
w3=w1-w2;
disp(w3);
disp('next mat');
end
k=k+1;
disp(k);
end
end
The upper bounds of your for-loops are the cause for your troubles.
You specify:
imh=size(ab,2);
imw=size(ab,1);
However, your for-loops have these conditions:
j=1:imh+wh-1
and
i=1:imw+ww-1
So you move past both the 'height' and the 'width' dimension.
Try this instead:
for j=1:imh-wh
for i=1:imw-ww
w1=ab(j:j+wh,i:i+wh,:);
w2=salt(j:j+wh,i:i+wh,:);
w3=w1-w2;
end
k=k+1;
end