X= [P(1,:,:);
P(2,:,:);
P(3,:,:)];
y= P(4:end,:);
indTrain = randperm(4798);
indTrain = indTrain(1:3838);
trainX= X(indTrain,:);
trainy = y(indTrain);
indTest = 3839:4798;
indTest(indTrain) = [];
testX = X(indTest,:);
testy = y(indTest);
It shows error in trainX= X(indTrain,:); saying
Index exceeds matrix dimensions
can anyone pls clarify ? thanks.
by the way I am having a 4x4798 data which my first 3 rows serve as predictors, and last row (4th row) is my response. how would i correctly split the data into the first 3838 columns as my training set and remaining as testing set.
Thanks..!!
Fix your error
To fix the indexing error you need to select the column indices of X, rather than the row indices:
trainX = X(:, indTrain );
Some words of advice
It seems like your P matrix is 4-by-4798 and it is two dimensional. Therefore, writing P(1,:,:) does select the first row, but it gives the impression as if P is three dimensional because of the extra : at the end. Don't do that. It's bad habit and makes your code harder to read/understand/debug.
X = P(1:3,:); % select all three rows at once
y = P(4,:); % no need for 4:end here - again, gives wrong impression as if you expect more than a single label per x.
Moreover, I do not understand what you are trying to accomplish with indTest(indTrain)=[]? Are you trying to ascertain that the train and test set are mutually exclusive?
This line will most likely cause an error since the size of your test set is 960 and indTrain contains 1:3838 (randomly permuted) so you will get "index exceeds..." error again.
You already defined your indTrain and indTest as mutually exclusive, no need for another operation. If you want to be extra careful you can use setdiff
indTest = setdiff( indTest, indTrain );
Related
I have two very large matrices (228453x460) and I want to compute correlation between rows.
for i=1:228453
if(vec1_preprocess(i,1))
for j=1:228453
df = effdf(vec1_preprocess(i,:)',vec2_preprocess(j,:)');
corr_temp = corr(vec1_preprocess(i,:)', vec2_preprocess(j,:)');
p = calculate_p(corr_temp, df);
temp = (meanVec(i)+p)/2;
meanVec(i) = temp;
end
disp(i);
end
end
This takes ~1day. Is there a direct way to compute this?
Edit: Code for effdf
function df = effdf(ts1,ts2);
%function df = effdf(ts1,ts2);
ts1=ts1-mean(ts1);
ts2=ts2-mean(ts2);
N=length(ts1);
ac1=xcorr(ts1);
ac1=ac1/max(ac1); % normalized autocorrelation
ac1=ac1(((length(ac1)+3)/2):((length(ac1)+3)/2+floor(N/4)));
ac2=xcorr(ts2);
ac2=ac2/max(ac2); % normalized autocorrelation
ac2=ac2(((length(ac2)+3)/2):((length(ac2)+3)/2+floor(N/4)));
df = 1/((1/N)+(2/N)*sum(((N-(1:length(ac1)))/N)'.*ac1.*ac2));
Since you didn't post the code, I assume that your custom functions calculate_p and effdf are perfectly optimized and don't represent the bottleneck of your script. Let's focus on what we have.
The first problem I see is:
if (vec1_preprocess(i,1))
A check over 228453 iterations can sensibly increase the running time. Hence, extract only the matrix rows that don't contain a 0 in the first column and perform your calculations on those:
idx = vec1_preprocess(:,1) ~= 0;
vec1_preprocess = vec1_preprocess(idx,:);
for i = 1:size(vec1_preprocess,1)
% ...
end
The second problem is corr. It seems like you are computing p-values also, using calculate_p. Why don't you use the buil-in p-values returned by the function as second output argument?
[c,p] = corr(A,B);
Alternatively, if Pearson's correlation is what you are looking for, you could replace corr with corrcoef to see if it produces a better performance.
Last but not least (in fact it's the most important thing): is there any reason why you are performing this computation row by row instead of running it on the whole matrices?
If you read the documentation, you'll see that corr computes the correlation between columns, not rows.
To convert rows into columns and columns into rows, simply transpose the matrix:
tmp1 = vec1_preprocess';
tmp2 = vec2_preprocess';
C = corr(tmp1,tmp2);
as a part of a bigger script i want to store data from a while loop in a matrix. I want to save parts of the COG_Ton_Av matrix which is 1738x3 in a new matrix. The COG_Ton_Av changes within every loop so i want to store the results outside. I have found multiple entries on how to store the data in a vector, but nothing for a matrix. What i tried is :
valuesforts= zeros(1000,3);
yr =1
while Qn>0
yindex = Gmhk*100
zindex = round(gs*100)
ts = (COG_Ton_Av ((zindex:yindex),:))
valuesforts(yr)=ts
yr = yr+1
end
I just posted parts of the while loop to make the question easier, I hope it is sufficient to answer the question.
While trying this i get following error:
Subscripted assignment dimension mismatch.
Error in cutoff_work14_priceescalation_and_stockpiling (line 286)
valuesforts(yr)=ts
The error means that ts is a different size to valuesforts (and it is indexed with yr as a vector.
If dimensions of TS vary on each iteration of the loop then use cell notation:
valuesforts = cell(<number of years>);
...
valuesforts{yr} = ts;
then the dimensions of ts won't matter.
To extract data also use { } e.g.
meanValues(yr) = mean(valuesforts{yr});
Bear in mind that the matrix within each cell of valuesforts will have same distentions as ts when it was assigned.
Alternatively, if TS is always the same size the pre-allocate valuesforts as:
valuesforts = zeros(<number of years>,<expected length of ts>,3);
...
valuesforts(yr,:,:) = ts;
Then depends on what you want to do with valuesforts.. reshape it or plot it.
In the worst case (not recommended), you can let the valuesforts grow with every loop iteration.
initialise with empty:
valuesforts=[];
then vertically append ts to valuesforts:
valuesforts = [valuesforts; ts];
this would give you a matrix with 3 columns and number of years * number of rows in ts in each loop iteration.
The below code gives me a error:
Subscripted assignment dimension mismatch.
Error in ==> lookmcvmt at 18
M(:,:,j,i) = mcmvOUT2((k+1):(k+Nz), i:Nt:Nr);
Please help to solve.
load MCMVout1xzy
mcmvOUT2 = MCMVout1xzy;
whos
[Nr2 Nr] = size(mcmvOUT2);
Ny = 51;
Nx = 51;
Nz = 41;
Nt = 10;
M = zeros(Nz,Nx,Ny,Nt);
for j=1:Ny
for i=1:Nt
k = Nz*(j-1);
M(:,:,j,i) = mcmvOUT2((k+1):(k+Nz), i:Nt:Nr);
end
end
The error 'subscripted assignment dimension mismatch' means you are trying to assign a block of values into a space that is the wrong size.
This entity
mcmvOUT2((k+1):(k+Nz), i:Nt:Nr);
represents a matrix of values in 2 dimensions. Its size is defined by the two ranges specified by (k+1):(k+Nz) and i:Nt:Nr - you can check its size by typing
size(mcmvOUT2((k+1):(k+Nz), i:Nt:Nr))
The space you are trying to fit it into has to be exactly the same dimensions. The size of the range specified by
M(:,:,j,i)
is defined by the Nz and Nx arguments to the zeros call with which you preallocated the array.
We can't test this because the MCMVout1xzy file containing your data is not given, but you will be able to solve this yourself by using the size command and making sure all your dimensions match.
Because matlab uses column-wise indexing, and a lot of us are used to the row-wise paradigm of cartesian coordinate systems, getting the order of your indexes right can be confusing - this is the root of a lot of these kinds of error (for me anyway).
Things to check: your dimensions Nz etc. are correct and the order of your Nz etc. variables in the zeros call is correct.
having a problem with my "new love", matlab: I wrote a function to calculate an integral using the trapz-method: `
function [L]=bogenlaenge_innen(schwingungen)
R = 1500; %Ablegeradius
OA = 1; %Amplitude
S = schwingungen; %Schwingungszahl
B = 3.175; %Tapebreite
phi = 0:2.*pi./10000:2.*pi;
BL = sqrt((R-B).^2+2.*(R-B).*OA.*sin(S.*phi)+OA.^2.*(sin(S.*phi)).^2+OA.^2.*S.^2.*(cos(S.*phi)).^2);
L = trapz(phi,BL)`
this works fine when i start it with one specific number out of the command-window. Now I want to plot the values of "L" for several S.
I did the following in a new *.m-file:
W = (0:1:1500);
T = bogenlaenge_innen(W);
plot(W,T)
And there it is:
Error using .*
Matrix dimensions must agree.
What is wrong? Is it just a dot somewhere? I am using matlab for the second day now, so please be patient.... ;) Thank you so much in advance!
PS: just ignore the german part of the code, it does not really matter :)
In your code, the arrays S and phi in the expression sin(S.*phi) should have same size or one of them should be a constant in order the code works
The error is most likely because you have made it so that the number of elements in schwingungen, i.e. W in your code, must be equal to the number of elements in phi. Since size(W) gives you a different result from size(0:2.*pi./10000:2.*pi), you get the error.
The way .* works is that is multiplies each corresponding elements of two matrices provided that they either have the same dimensions or that one of them is a scalar. So your code will work when schwingungen is a scalar, but not when it's a vector as chances are it has a different number of elements from the way you hard coded phi.
The simplest course of action (not necessarily the most Matlabesque though) for you is to loop through the different values of S:
W = (0:1:1500);
T = zeros(size(W); %Preallocate for speed)
for ii = 1:length(W)
T(ii) = bogenlaenge_innen(W(ii));
end
plot(W,T)
In your function you define phi as a vector of 10001 elements.
In this same function you do S.*phi, so if S is not the same length as phi, you will get the "dimensions must agree" error.
In your call to the function you are doing it with a vector of length 1501, so there is your error.
Regards
I am trying to model a vector data containing 200 sample points denoting a measurement.I want to see "goodness of fit" and after reading I found that this can be done by predicting the next set of values(I am not that confident though if this is the correct way).I am stuck at this since the following code gives an error and I am just unable to solve it.Can somebody please help in removing the error
Error using *
Inner matrix dimensions must agree.
Error in data_predict (line 27)
ypred(j) = ar_coeff' * y{i}(j-1:-1:j-p);
Also,can somebody tell me how to do the same thing i.e get the coefficients using nonlinear AR modelling,moving average and ARMA since using the command nlarx() did not return any model coefficients?
CODE
if ~iscell(y); y = {y}; end
model = ar(y, 2, 'yw');
%prediction
yresiduals=[];
nsegments=length(y);
ar_coeffs = model.a;
ar_coeff=[ar_coeffs(2) ar_coeffs(3)]
for i=1:nsegments
pred = zeros(length(y{i}),1);
for j=p+1:length(y{i})
ypred(j) = ar_coeff(:)' * y{i}(j-1:-1:j-p);
end
yresiduals = [yresiduals; y{i}(p+1:end) - ypred(p+1:end)];
end
In matlab, * is the matrix product between two matrices. That means that the number of columns in the first matrix must equal the number of rows in the second matrix. You might have intended to use .* element by element multiplication. EDIT: For element by element multiplication, the matrices must be the same size. Check the size of your matrices. If they don't fit either of these conditions, something needs to change.