MatLab Power Law, Non-Positive Value Error - matlab

Hi I'm trying to fit a power model to my data using MatLab's fit function
fo = fit(log2(x(:)),log2(y(:)),'power1');
plot(fo,'g'), hold on
However when I run this I get the error
Error using fit>iFit (line 282)
Cannot fit Power functions to data where X has nonpositive values.
Error in fit (line 108)
[fitobj, goodness, output, convmsg] = iFit( xdatain, ydatain, fittypeobj, ...
Error in CurvedPowerLaw (line 20)
fo = fit(log2(x(:)),log2(y(:)),'power1');
When looking at my data and checking if any are less than 1, nothing is displayed
x(count_1)=M(i,1);
y(count_1)=M(i,2);
count_1= count_1+1;
if(M(i,2)<1)
display(M(i,1))
end;
M is a csv file with two columns. I also re ran the code for
if(M(i,1)<1)
and nothing was displayed. Checking manually and nothing seemed to be below 1 either.
i is just the line in the file that is being parsed. The file looks like
344,17
345,13
346,13
347,16
340,12
M(i,1) will result in returning one of the >300 numbers and M(i,2) will return ~10 value
Any help would be much appreciated!!
Thanks

While all values that were parsed in were >0 when scaling them by log2 that's where the 0 values started appearing. A quick fix was to add 1 to each value when parsing them in.

Related

Error during rm ANOVA (Matlab) with random factor

I am trying to run a repeated measures ANOVA in Matlab with 4 factors including one factor representing my subjects which I want as a random factor.
The code I have is as follows:
[p,table,stats] = anovan(COORDS_SUBJ_II,{group_hand,group_stim,group_time,group_subs},'random',4,'varnames',{'HAND','STIM','TIME','SUBS'});
Here, all variables have the same dimension, which is 1350x1(all types are 'double'). I checked my code with some proposed code on the net and it matches, yet I keep getting the following error...
Error using chi2inv (line 3)
P and V must be of common size or scalars
Error in anovan>varcompest (line 838)
L = msTerm .* dfTerm ./ chi2inv(1-alpha/2,dfTerm);
Error in anovan>getRandomInfo (line 811)
[varest,varci] = varcompest(ems,randomterms,msTerm,dfTerm,alpha);
Error in anovan (line 296)
getRandomInfo(msterm,dfterm,mse,dfe,emsMat,randomterm,...
My dependent variable (COORDS_SUBJ_II) has a couple of NaN's in it, although I ran the code once where I replaced those NaN's with random numbers and it still gives me the same error. I am kind of lost right now and would appreciate any help.
Best
ty
Found it out. A toolbox I downloaded a while ago also had the chi2inv command and this prompted the error =)

Predictor and response variables must have the same length; Matlab

Thanks in advance for the help.
I am trying to use stepwise regression on a set of data. I have the data in a table, with the single predictor variable on the far right of the table (as a column). Here is what my code looks like.
mdl = stepwiseglm(dummyTrainingTable,'modelspec',modelTech,'Criterion',criterion);
where modelTech and criterion are variables that hold strings dictating two name-value pair options. I am getting the following error
Error using classreg.regr.FitObject/assignData (line 257)
Predictor and response variables must have the same length.
Error in classreg.regr.TermsRegression/assignData (line 349)
model =
assignData#classreg.regr.ParametricRegression(model,X,y,w,asCat,varNames,excl);
Error in GeneralizedLinearModel/assignData (line 794)
model =
assignData#classreg.regr.TermsRegression(model,X,y,w,asCat,dummyCoding,varNames,excl);
Error in GeneralizedLinearModel.fit (line 1165)
model =
assignData(model,X,y,weights,offset,binomN,asCatVar,dummyCoding,model.Formula.VariableNames,exclude);
Error in GeneralizedLinearModel.stepwise (line 1271)
model = GeneralizedLinearModel.fit(X,y,start.Terms,'Distribution',distr,
...
Error in stepwiseglm (line 148)
model = GeneralizedLinearModel.stepwise(X,varargin{:});
This doesn't make sense to me since clearly my response and predictor variables have the same length; they're in a table together. If they weren't the same length, they couldn't be in a table right? Is this an issue with Matlab or is there just something simple that I am missing?
Note, I when I convert the table to a matrix, stepwiseglm runs just fine. i.e.,
dummyTrainingArray = table2array(dummyTrainingTable);
mdl = stepwiseglm(dummyTrainingArray(:,1:size(dummyTrainingArray,2) - 1), dummyTrainingArray(:,size(dummyTrainingArray,2)),modelTech,'VarNames', ...
dummyTrainingTable.Properties.VariableNames,'Criterion', criterion);
I figured out a solution. Although the documentation online states that the input can be a table, when I checked the manual within my version of Matlab (run 'help stepwiseglm'), I found that the function was compatible only with datasets. I then converted my table to a dataset and it ran fine.
Edit, I have Matlab version
8.2.0.701 (R2013b)
'modelspec' is not a valid argument name for the function. Try:
mdl = stepwiseglm(dummyTrainingTable, modelTech, 'Criterion', criterion);

Looping over a Matlab structure

I am currently processing a bunch of files that I have imported into a structure, but have hit a bump in the road while trying to loop over the data.
First of all, here is my structure:
Ice
1.1 az160, az240, az300...
1.1.2 zen15, zen30,zen45...
1.1.2.1 Data
1.1.2.2 Textdata
I am trying to extract a value from each "textdata" cell array and use it to divide a column in data of the same structure. To do so, I am looping through the structure in the following way:
az_names = fieldnames(ice)
for m = 1:numel(az_names)
snames = fieldnames(ice.(az_names{m}))
for k = 1:numel(snames)
inttime = strrep(ice.(az_names{m}).(snames{k}).textdata(9,1), 'Integration > Time (usec): ','');
inttime = strrep(inttime, ' (USB2+E00040)','');
integration = cellfun(#str2num,inttime)
line 17 ice.(az_names{m}).(snames{k}).data(:,4) = ice.(az_names{m}).(snames{k}).data(:,3)/integration
end
end
I get the following error:
Index exceeds matrix dimensions
Edit: Matlab gives me the error at line 17. If I run the code up to "integration" and also write:
ice.(az_names{m}).(snames{k}).data(:,4)
I don't get a problem, Matlab prints to screen the right number and the data column.
I thought this would loop through each field in the structure and do the operation (dividing a column of values by a number), but I seem to be missing a point here. Can anybody see my mistake?
Regards,
If the error occurs when you try to execute this fragment:
ice.(az_names{m}).(snames{k}).data(:,4)
Then the cause is quite simple.
The variables m and k seem to be handled properly (due to the numel in the loop they should never be too big), meaning that the 4 is simply too big.
Check
ice.(az_names{m}).(snames{k}).data(:,4)
Or even more directly
size(ice.(az_names{m}).(snames{k}).data)
And you should find that the second element of the size is less than 4.
On second thought, this fragment works:
a.b=1;
a.b(:,4)=1
So I suspect that the error occurs when trying to read in this part:
ice.(az_names{m}). (snames{k}).data(:,3)
Meaning that the second element of the size should even be less than 3.
Also I would recommend removing the space.

reshape() command causes an error being evoked from a loop

psz=length(pic)
p=0; %masking counter
for i=1:outs:(psz) % dividing in blocks
for j=1:outs:(psz)
p=p+1
blocks(:,:,p)=pic(i:i+outs-1,j:j+outs-1);
ins(:,p)=reshape(blocks(:,:,p)',1,ins')';
end
end
so to begin with i am trying to reproduce sanger rule for pca using neural networks so if somone wants to duscuss about it or give him my code he can message me:)
i get the following error
Error using reshape
To RESHAPE the number of elements must not change.
Error in train (line 30)
ins(:,p)=reshape(blocks(:,:,p)',1,ins')';
Converting blocks(:,:,p) to a column vector should clear the error as long as there are the same number of elements in blocks(:,:,p) as the row length of ins
col_vec = blocks(:,:,p);
ins(:,p) = col_vec(:);
The size of blocks(:,:,p) is outs-by-outs so to make a column vector, it has to be (outs*outs)-by-1. To do this, the command would be:
ins(:,p)=reshape(blocks(:,:,p)',outs*outs,1); % no need for '
However, be sure that size(ins,1) is outs*outs or it won't work. What is the size of ins (and blocks, out of curiosity)? Also, be sure you really want the ' on blocks because the command will work with or without it.

Error "Index exceeds matrix dimensions." in MatLab using importdata with a text file

I'm trying to import data using importdata and when I try to parse the returned data to create a matrix I get, "Index exceeds matrix dimensions". Below is my code...
traindata = importdata('textfile.txt');
%[A,delimiterOut,headerlinesOut] = importdata('textfile.txts');
disp(traindata); %everytime I run this code traindata increments by 1
X = traindata(' ',1:8); %this is where the error occurs, delimiter is 3 spaces
Y = traindata(' ',9);
Below is the format of the data in textfile.txt...
,,,5.4,,,0.0,,,0.0,,,1.6,,,2.5,,,1.0,,,6.7,,,2.8,,,6.1
,,,4.2,,,1.1,,,3.6,,,3.9,,,1.8,,,9.3,,,3.3,,,2.4,,,7.6
The data is delimited by spaces (I used commas to try and show the spaces between the data) and a newline at the end of each line. I've open textfile.txt in word and verified by viewing the hidden formatting characters. I've tried the code...
[A,delimiterOut,headerlinesOut] = importdata(inputfile);
to try to verify the delimiter used and I get the error, "Too many output arguments." As you can see I'm trying to create two matrices (X,Y) from the imported data. I've seen this specific error on stackoverflow but nothing regarding importdata. I've also tried dlmread and have not had luck. Thanks in advance for any help.
Tried the suggestion of importing the data using file->import data but I receive the error..
Error using importdata
Too many output arguments.
"Error in uiimport/runImportdata (line 433)
[datastruct, OTextDelimiter, OHeaderLines] = ...
Error in uiimport/gatherFilePreviewData (line 376)
[datastruct, textDelimiter, headerLines]= runImportdata(fileAbsolutePath, type);
Error in uiimport (line 194)
[ctorPreviewText, ctorHeaderLines, ctorDelim] = ..."
I'm starting to wondering if it's some sort of application bug. Here are some specifics..
"R2012a (7.0.14.739) 64 bit (Win64)". The encoding of the text file is utf-8. Thanks again for the help!
Looks like the array returned from importdata is a 1 element array.
train = importdata('textfile.txt');
fprintf('1st element in array %d\n', traindata(1)); % prints a number a number that increase each time I run this function ie 1,2,3,4...
fprintf('2nd element in array %d\n', traindata(2)); % produces error, "Index exceeds matrix dimensions"
I often find it useful to use matlab's built in GUI for importing a data file, which can help to visualised how the data will be imported. There is an option in here to produce the code required to replicate the options that were selected during the import which will allow you to work out how to dynamically import the data.
Just go to:
File >>> Import Data...