필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Problems in accessing 2nd column of matrix

조회 수: 2 (최근 30일)
Mich
Mich 2015년 4월 11일
마감: MATLAB Answer Bot 2021년 8월 20일
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
Mich
Mich 2015년 4월 11일
편집: Mich 2015년 4월 11일
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 2015년 4월 13일
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

답변 (2개)

Geoff Hayes
Geoff Hayes 2015년 4월 11일
편집: Geoff Hayes 2015년 4월 11일
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
Mich
Mich 2015년 4월 11일
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 2015년 4월 11일
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 2015년 4월 11일
편집: pfb 2015년 4월 11일
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
Mich
Mich 2015년 4월 11일
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)

이 질문은 마감되었습니다.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by