undefined function 'minus' for input arguments of type table

I have a .txt (220 rows * 12 cols, first row text, rest of the file is decimal numbers) file, first I converted it to a table: T = readtable('xxx.txt','Delimiter','\t') and then saved T to .mat form.
then I ran these coads: >> load T.mat >> col=0; raa=0.05; d=0; c=0; n=0; w=0; b=0; data4=zeros(220,10);
for col=3:12
for i=1:220
b=0;
c=0;
for j=1:220
if i~=j
d=sqrt((T(i,1)-T(j,1))^2+(T(i,2)-T(j,2))^2);
if d<30
c=(1/(d^2))*T(j,col);
b=b+c;
else
end
end
end
x=T(i,col) - (raa * b);
n=i;
data4(i,col)= x;
data4(i,1)=n;
end
% dlmwrite('trash.txt',data4,'\t') dlmwrite('T-100spreg2-raa05.txt',data4,'\t') end
I got an error message Error: undefined function 'minus' for input arguments of type table.
My questions are: 1. why when I checked the size of T I got ans=1 1, why not ans=220 12?
2. why I got this error message?
3. If I shouldn't use table T directly in the coad, then I need to convert it to an array. I used A=table2array('T') and got: Error: Cell contents reference from a non cell array object Error in table2array (line 27) a = t{:,:}; Could it be I did something wrong from the first step i.e. convert .txt to table (thus I got ans=1 1 but not 220 12)?

Réponses (2)

Michael Haderlein
Michael Haderlein le 17 Sep 2014
Modifié(e) : Michael Haderlein le 17 Sep 2014
With readtable, you get a Table object. This is the reason for the errors. If you use dlmread instead, you'll directly get an array which has the right size and minus is defined for it. I cannot comment on your third question as this seems to refer to lines which you haven't posted.

4 commentaires

As long as I use dlmred, I got an error message to say it is problematic when reading numbers==>x y DEM asp grad DAI2 HI VI SLI2 Yield DAI1 SLI1\n. This is the first row of my .txt file. Maybe I should escape the first row but Im running out of time for this assigment so I just converted table to array.
So I used A=table2array(T) converted my table T to array A, and used A in the coad to get different .txt files with different raa values. It worked fine if i.e. raa=0.01 or 0.08, but as long as I assing 0:0.01:1 to raa, I got error message: Subscripted assignment dimension mismatch. What could be the reason for it? Thanks!!!
Yes, easiest in my opinion would have been to skip the first line (dlmread supports that). Do I get it right that you have managed it to solve the above mentioned problems?
The problem you now report is the following: If raa is an array, then
x=T(i,col) - (raa * b);
produces an array as well. But in this line
data4(i,col)= x;
you try to save an entire array in a single array element - that cannot work. I'm not familiar enough with your program to present a solution, but maybe something like data4(:,col)=x or data4(i,:)=x might be the right way.
Didn't work with either data4(:,col)=x or data4(i,:)=x Error: unexpected matlab operator Maybe I need to change something else?
I'm sure you need to change something else, but I cannot tell you what. I don't know what's raa, x, data4 and so on. I just can tell you that raa (and x) will have 101 elements which you want to save in data4, but I don't know what they mean.
Anyway, I think there's more wrong with your code. First align your code properly (ctrl+a, ctrl+i). Then you'll see that
x=T(i,col) - (raa * b);
is after the end of the for loop which iterates over i. Thus, you always write T(220,col). Is that what you want? Same holds for the data4 lines.
In any case, I would think about vectorizing the code. I suppose the central line is
d=sqrt((T(i,1)-T(j,1))^2+(T(i,2)-T(j,2))^2);
which is just giving the distance between all points with x positions in the first column and y positions in the second column, right? Then, this line
d=sqrt(bsxfun(@minus, T(:,1),T(:,1)').^2+bsxfun(@minus,T(:,2),T(:,2)').^2);
should do the same (if I didn't mess up something). But this now works without any loop. I'm sure the rest of the code can also be vectorized, however, I'm sure parts of the code are missing as e.g. col will always be 0 which is not a valid index.

Connectez-vous pour commenter.

You can use curly braces {} to index into the elements of a table
T{3,4}

1 commentaire

I am using array A instead of table T now, it works. But now the problem is if raa=0:0.01:1, how could I re-write the code so it works? Otherwise I just keep getting error message: unexpected matlab operator (even after I tired Michael Haderlein's advice).
col=0; raa=0:0.01:1; % I made change here. d=0; c=0; n=0; w=0; b=0; data4=zeros(220,10);
for col=3:12
for i=1:220
b=0;
c=0;
for j=1:220
if i~=j
d=sqrt((A(i,1)-A(j,1))^2+(A(i,2)-A(j,2))^2);
if d<30
c=(1/(d^2))*T(j,col);
b=b+c;
else
end
end
end
x=A(i,col) - (raa * b);
n=i;
data4(i,col)= x;
data4(i,1)=n;
end
% dlmwrite('trash.txt',data4,'\t') dlmwrite('T-100spreg2-raaX.txt',data4,'\t') end
Thanks!

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by