Error with table subtraction
1 次查看(过去 30 天)
显示 更早的评论
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
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
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 个评论
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
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!