Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

Problems in accessing 2nd column of matrix

1 visualización (últimos 30 días)
Mich
Mich el 11 de Abr. de 2015
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
I know this is a very simple problem, but I cant seem to find what is the issue as it seems logical.
I tried instances when xx is a column vector or horizontal vector, but it doesnt make a difference.
Count is the final number of data points i want to obtain. E.g.
count = 10
for k = 1:(length(xx)-1)
temp = *data* (xx(k):xx(k+1));
mat(:,k) = interrp(temp, count);
end
interrp is a function that was written to use the i
interp1 function to use the method 'spline'.
My code always stop after finishing filling up the 1st column. This is the error that I receive: Subscripted assignment dimension mismatch.
  2 comentarios
Mich
Mich el 11 de Abr. de 2015
Editada: Mich el 11 de Abr. de 2015
just to make things clearer, this is my interrp function
function result=interrp(data,Max_number)
n=length(data);
xx=1:n/Max_number:n;
y=data;
result=interp1(y,xx,'spline');
Mich
Mich el 13 de Abr. de 2015
I realised that the errors lies in the in-built interp1 function. It does not seem to be able to interpolate and obtain the length desired (e.g. length of xx). Any one knows how to solve this error? the error seems to occur randomly as it works when i run the code on different datasets

Respuestas (2)

Geoff Hayes
Geoff Hayes el 11 de Abr. de 2015
Editada: Geoff Hayes el 11 de Abr. de 2015
Mich - what are the dimensions of the array that the interrp function is returning? The error message Subscripted assignment dimension mismatch typically means that the code is trying to insert more data than it can fit into the destination array. For example,
x = zeros(3,42);
x(:,2) = randi(255,4,1);
returns the same error because I am trying to assign a 4x1 matrix into a 3x1 destination. Put a breakpoint at the line
mat(:,k) = interrp(temp, count);
then run your code and verify that what interrp is returning is too large for the mat(:,k) destination.
You may also want to pre-size mat prior to entering the for loop so that you know exactly what you are trying (and what you are able) to assign to this matrix.
  2 comentarios
Mich
Mich el 11 de Abr. de 2015
Hi Geoff,
interrp function return a horizontal array of dimensions (1, count).
I've tried to presize mat and clearing any earlier allocation within mat by doing this:
clearvars mat mat = []; mat = zeros(count,length(xx)-1);
I'm not sure how to put a break at the code with the "interrp'. However, I include size(mat) before and after the line with interrp function, and realised that the main problem lies because mat(:,2) cant be accessed when k = 2.
Geoff Hayes
Geoff Hayes el 11 de Abr. de 2015
Mich - see http://blogs.mathworks.com/videos/2010/09/02/using-debugger-to-walk-through-code to step through the code using the debugger.

pfb
pfb el 11 de Abr. de 2015
Editada: pfb el 11 de Abr. de 2015
Hi,
Your code is very obscure. What exactly do you want do attain? In particular, this line
temp = xx(xx(k):xx(k+1));
looks weird, especially if xx does not contain positive integers. Even in that case, I kind of see why matlab complains about the dimension mismatch. What is xx?
  1 comentario
Mich
Mich el 11 de Abr. de 2015
Hi pfb,
this is a simplified version of the code. xx is actually a row array with positive integer. It includes the indices which i'll like to split each column in mat.
E.g. if xx = [3 6 9 15];
for k = 1:(length(xx)-1) temp = data(xx(k):xx(k+1)); mat(:,k) = interrp(temp, count); end
what i want to get is : data(3) data(6) data(9) : : : : : : data(6) data(9) data(15)
but the interrp function would have made everything the same lenght, so it can be put into a matrix. Now the problem i have is getting the for loop to access mat(:,2)

La pregunta está cerrada.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by