Error with table subtraction

조회 수: 1 (최근 30일)
Mukund
Mukund 2018년 1월 4일
편집: Guillaume 2018년 1월 4일
At the line d1, error appears that '-' is undefined operator. If bsxfun is used, error on numeric arrays is shown. Conversion of table type (before and after d1) leads to different errors. Please, suggest suitable modification with data format table.
a=[0.5;0.6;0.9;1.0;1.6]
b=[2.0;1.5;1.3;2.8;3.2]
c=[-0.1;0.4;0.5;1.1;1.4]
T=table(a,b,c)
for i=2:size(T,1)
if T{i,'a'}>T{i-1,'a'}
d=min(T{i,:})
else
d=0
end
d1=sum(d-T,2)
end
  댓글 수: 1
Guillaume
Guillaume 2018년 1월 4일
편집: Guillaume 2018년 1월 4일
To suggest suitable modification we would first need to know what it is you intended doing. As it is, your loop is pointless since it overwrites d at each step, so only the last iteration of the loop will have an effect on d.
subtraction is not defined for tables, so once again it's not clear what you intended to do with your last line, nor why you're using a table in the first place.
Also note that your table has only one row. Did you mean
T = table(a', b', c', 'VariableNames', {'a', 'b', 'c'})
instead?

댓글을 달려면 로그인하십시오.

채택된 답변

Birdman
Birdman 2018년 1월 4일
편집: Birdman 2018년 1월 4일
First, change the first three line with their transposes.
a=[0.5 0.6 0.9 1.0 1.6].'
b=[2.0 1.5 1.3 2.8 3.2].'
c=[-0.1 0.4 0.5 1.1 1.4].'
Then, change the line
d1=sum(d-T,2)
to
d1=sum(d-table2array(T),2)
  댓글 수: 4
Mukund
Mukund 2018년 1월 4일
@Guillaume.Table shall remain in the code, as it holds different variables and used at many places in my code. This is small part of it. For loop according to you can be avoided. Alright, so you please resolve the subtraction issue and that without for loop.If it works, it is also Welcome.
Guillaume
Guillaume 2018년 1월 4일
편집: Guillaume 2018년 1월 4일
If you're converting a table to an array that's a good indication that a table is the wrong container for your data. The fact that you're also summing across the variables and taking the minimum across the variables is also a good indication that there is actually no difference between the columns.
As I keep saying, the for loop overwrites the result d and d1 at each step, so only the last iteration has any effect. So, I'm assuming you made a mistake in your original code and meant to index either d or d1 or maybe both. Until you clarify what the result is supposed to be it's difficult to tell you how to do it cleanly.
The fact that with your example data, the if is always true also doesn't help.
As per my answer, you could calculate all the d values generated by your loop in just one line, without the loop. If you insist on using tables, this would be:
D = min(table2array(T), [], 2) .* [0; diff(T.a) > 0]
where D(i) is the d you calculated at step i.
If a different d1 is supposed to be calculated at each step of your original loop, then these could be obtained in just one line as:
D1 = sum(D.' - permute(table2array(T), [1 3 2]), 3);
%or more simply, since D can be taken out of the sum:
%D1 = D.'*width(T) - sum(table2array(T), 2)
where D1(:, i) is the d1 you calculated at step i.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Guillaume
Guillaume 2018년 1월 4일
A complete guess, since you haven't explained what you're trying to do:
a=[0.5 0.6 0.9 1.0 1.6];
b=[2.0 1.5 1.3 2.8 3.2];
c=[-0.1 0.4 0.5 1.1 1.4];
m = [a;b;c]';
d = min(m, [], 2);
d([true; diff(m(:, 1)) <= 0]) = 0;
%these last two lines could also be written as the slightly more obscure one-liner:
% d = min(m, [], 2) .* [0; diff(m(:, 1)) > 0];
d1 = sum(d-m, 2)
Using a table for this does not make sense.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by