Error with table subtraction

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 个评论

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 个评论

Thanks. Row d1 was culprit. You pointed it rightly.
Row d1 was culprit.
If only that was the only problem! As pointed out in my comment to the question, the loop is pointless as only the last iteration has any effect.
In any case, the accepted solution convert the table into a matrix. If you're doing matrix calculations, then you shouldn't be using tables. They're not the same and are not designed for the same purpose. Using a table in this case just complicates everything.
And of course, even if the result obtained in the loop was meant to be indexed, the whole thing could be done more efficiently without the loop.
@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.
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 个)

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.

类别

帮助中心File Exchange 中查找有关 Logical 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by