Error with table subtraction

1 view (last 30 days)
Mukund
Mukund on 4 Jan 2018
Edited: Guillaume on 4 Jan 2018
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 Comment
Guillaume
Guillaume on 4 Jan 2018
Edited: Guillaume on 4 Jan 2018
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?

Sign in to comment.

Accepted Answer

Birdman
Birdman on 4 Jan 2018
Edited: Birdman on 4 Jan 2018
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 Comments
Mukund
Mukund on 4 Jan 2018
@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 on 4 Jan 2018
Edited: Guillaume on 4 Jan 2018
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.

Sign in to comment.

More Answers (1)

Guillaume
Guillaume on 4 Jan 2018
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.

Products

Community Treasure Hunt

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

Start Hunting!