well, I am new to matlab programming and I have been battling on the indexing issues. I am currently working on image processing which so far drive me crazy. anyways, lets jump to the questions.
I have the following code
perm=randperm(size(X,2));
CX=X(:,perm(1:nclus));
I tried to run the code but it triggers an error saying " Index exceeds the matrix dimensions. To my humble knowledge I think it is because the (:,perm(1:nclus)) is higher than the matrix dimensions. I would like to know how can i solve this problem.
Note that X: is the input points in the columns
nclus: number of clusters.
I highly appreciate if you guys clarify to me the error cause and the possible solution for it.
Thank you
Sami
Guessing that you just want to get nclus random columns from a 2 dimensional matrix X, try this:
perm=randperm(size(X,2));
CX=X(:,perm<=nclus);
The error that you experience should not come from X being called with too many dimensions, it is probably because the dimensions of perm are exceeded. Try running this line by line:
perm = randperm(size(X,2)); %Should be ok
idx = perm(1:nclus); %Probably fails
X(:,idx)
Related
after using gnuplot for years and experiencing many user-related issues, I thought I'd finally know how to fit a function to a dataset.
Today I tried to fit a simple
y = m * x² + b
function. However, gnuplot did not change my 'm' value. It does change 'b' to the correct value however.
I have my dataset uploaded and here is my gnuplot script with which I'm trying to fit, maybe someone can reproduce this on his machine and confirm, that it is not a fault of my computer but some kind of faulty code in the script, or it may even be a bug (I highly doubt that).
set xtics 0.000001
set format x '%10.1E'
set xrange [0:2E-07]
#fit
f(x)=a*(x**2)+b
a=380812
b=1
fit [0:2E-07] f(x) 'GDAMitte1.txt' using ($1+7.6E-06):2 via a,b
plot 'GDAMitte1.txt' using ($1+7.6E-06):2, f(x)
I've pasted the dataset here: http://www.heypasteit.com/clip/29LU
I'd be very thankful for an answer to that, even if it's just a confirmation, that it doesn't fit on your machine as well. Thank you.
Btw: The initial value I've set is pretty much the one it has to be after the fit, but it's not as exact of course. Should be good enough though for gnuplot to get where to go to.
This is because two parameters are of greatly different magnitude, check help fit tips.
You should replace the function with one that has a prefactor built in:
f(x) = a *1e5 * (x**2)+b
a=3.8 # instead of 380000
b=1
fit ....
From gnuplot version 5.0 on, gnuplot by default internally prescales all parameters, so this problem with calculating the residuals should no longer occur for any function, provided your initial values are not off too much.
I'm using Genetic Algorithm & because my dataset is a little bad, It does not converge to the minimum & I receive this error:
(***Note: Before the error, I see this comment on command window:
Length of label vector does not match # of instances.
***Note:
When I run it for linear kernel function in SVM it's OK. but when I run it for RBF,Polynomial,Sigmoid kernel function, it happens.)
Matlab System Error:
Matlab has encountered an internal problem and needs to close.
my code is like this:
(GA part):
x = ga(#(nu) nu_svm_rbf( nu,train_classset,train_dataset,test_classset,test_dataset ),1,[],[],[],[],lb,ub);
nu_optimum_svd_rbf(1,i) = x;
What should I do by this sentence:
Length of label vector does not match # of instances.
What does it mean? it occurs before running the GA.
Would you please help me?
Thanks
Note: I'm using libSVM & my dataset size is 1000 points with 10 features.
Use the options argument.
You probably want gaoptimset('Generations', 1000)
I have a matrix S(105 rows and 22 columns) and I need to find its orthogonal (when I multiply S with the orthogonal the result must be a zero matrix).I searched and the only command I found that seems to do what I want is nullspace[S] but the result is not the matrix I need.It is a matrix with 8 rows and 22 columns that it doesnt give me the result I want.I tried Transpose in case it got the matrix backwards but the multiplication cannot be done either.Is there anyone who knows about mathematica that can help me?Thanks.
I am not sure, if I understood your concept of an "orthogonal" matrix, which is usually defined differently. But if you are looking for a matrix T such that T.S == {{0,0,....},...} then
T = NullSpace[Transpose[S]];
Unless your 105*22-dimensional matrix S is highly degenerate, there is no solution such that S.T==0.
In this case, T = Transpose[NullSpace[S]] will most likely render {}.
I saw some similar issues in this forum but I didn't find a real solution to this problem.
I have the following matlab code, in which I work with very big images (182MP):
%step 1: read the image
image=single(imread('image.tif'));
%step 2: read the image segmentation
regions=imread('image_segmentation.ppm');
%step 3: count the number of segments
number_of_regions=numel(unique(regions));
%step 4: regions label
regions_label=unique(regions);
for i=1:number_of_regions
%pick the pixel indexes of the i'th region
[x_region,y_region]=find(regions==label_regions(i));
%the problem starts here
ndvi_region=(image(x_region,y_region,1)-image(x_region,y_region,3))./(imagem(x_region,y_region,1)+image(x_region,y_region,3));
every time I run the code with specific regions matlab returns the error: Maximum variable size allowed by the program is exceeded.
I'm running the code with 48GB of RAM in my colllege's cluster. The problem starts only in region number 43 and below. The other regions run ok.
Is there a smart way for me to run this code?
I believe the problem is in your use of
image(x_region,y_region,1)
I suspect that you think that accesses the N elements of the region; but in fact, you access NxN elements! For a large region, that can easily blow up on you. In general, A(vec1, vec2) creates a section of numel(vec1) by numel(vec2). To solve this, you need to use the sub2ind function to find just the indices you need (or use a single parameter in your find command, and shape your matrix accordingly):
% option 1
[x_region, y_region]=find(regions==label_regions(i));
indx1 = sub2ind(size(image), x_region, y_region, 1*ones(size(x_region)));
indx3 = sub2ind(size(image), x_region, y_region, 3*ones(size(x_region)));
ndvi_region = (image(indx1) - image(indx3))./(image(indx1) + image(indx3));
% option 2
indx = find(regions==label_regions(i));
r_image = reshape(image, [], 3); % assuming you have XxYx3 image
ndvi_region = (r_image(indx, 1) - r_image(indx, 3))./(r_image(indx,1) + r_image(indx, 3));
The second option does make a complete copy of the image, so option 1 is probably quicker.
Suppose that I have these Three variables in matlab Variables
I want to extract diverse values in NewGrayLevels and sum rows of OldHistogram that are in the same rows as one diverse value is.
For example you see in NewGrayLevels that the six first rows are equal to zero. It means that 0 in the NewGrayLevels has taken its value from (0 1 2 3 4 5) of OldGrayLevels. So the corresponding rows in OldHistogram should be summed.
So 0+2+12+38+113+163=328 would be the frequency of the gray level 0 in the equalized histogram and so on.
Those who are familiar with image processing know that it's part of the histogram equalization algorithm.
Note that I don't want to use built-in function "histeq" available in image processing toolbox and I want to implement it myself.
I know how to write the algorithm with for loops. I'm seeking if there is a faster way without using for loops.
The code using for loops:
for k=0:255
Condition = NewGrayLevels==k;
ConditionMultiplied = Condition.*OldHistogram;
NewHistogram(k+1,1) = sum(ConditionMultiplied);
end
I'm afraid if this code gets slow for high resolution big images.Because the variables that I have uploaded are for a small image downloaded from the internet but my code may be used for sattellite images.
I know you say you don't want to use histeq, but it might be worth your time to look at the MATLAB source file to see how the developers wrote it and copy the parts of their code that you would like to implement. Just do edit('histeq') or edit('histeq.m'), I forget which.
Usually the MATLAB code is vectorized where possible and runs pretty quick. This could save you from having to reinvent the entire wheel, just the parts you want to change.
I can't think a way to implement this without a for loop somewhere, but one optimisation you could make would be using indexing instead of multiplication:
for k=0:255
Condition = NewGrayLevels==k; % These act as logical indices to OldHistogram
NewHistogram(k+1,1) = sum(OldHistogram(Condition)); % Removes a vector multiplication, some additions, and an index-to-double conversion
end
Edit:
On rereading your initial post, I think that the way to do this without a for loop is to use accumarray (I find this a difficult function to understand, so read the documentation and search online and on here for examples to do so):
NewHistogram = accumarray(1+NewGrayLevels,OldHistogram);
This should work so long as your maximum value in NewGrayLevels (+1 because you are starting at zero) is equal to the length of OldHistogram.
Well I understood that there's no need to write the code that #Hugh Nolan suggested. See the explanation here:
%The green lines are because after writing the code, I understood that
%there's no need to calculate the equalized histogram in
%"HistogramEqualization" function and after gaining the equalized image
%matrix you can pass it to the "ExtractHistogram" function
% (which there's no loops in it) to acquire the
%equalized histogram.
%But I didn't delete those lines of code because I had tried a lot to
%understand the algorithm and write them.
For more information and studying the code, please see my next question.