Creating a new table column that is the range of numbersbetween two other column

1 次查看(过去 30 天)
OriF
OriF2021-10-28
评论: Rik ,2021-11-2
Hi,
I'm trying to add a new column to a table, that each row is the range between two numbers corresponding to two other existing columns.
Somthing like this:
K>> RESULTS_T
RESULTS_T =
2091×4 table
y_tl x_tl y_br x_br
____ ____ ____ ____
54 119 60 129
177 119 183 129
300 119 306 129
423 119 429 129
546 119 552 129
669 119 675 129
792 119 798 129
#########################################
y_range
y_tl : y_br
#########################################
I manage to do so only for a single row:
K>> {RESULTS_T{:,1} : RESULTS_T{:,3}}
ans =
1×1 cell array
{[54 55 56 57 58 59 60]}

回答(1 个)

Rik
Rik 2021-10-28
You can either use a loop, or hide the loop with rowfun:
data=[54 119 60 129
177 119 183 129
300 119 306 129
423 119 429 129
546 119 552 129
669 119 675 129
792 119 798 129];
ytl=data(:,1);
ybr=data(:,3);
RESULTS_T =table(ytl,ybr);
[RESULTS_T rowfun(@(ytl,ybr)ytl:ybr,RESULTS_T,'OutputVariableNames','range') ]
ans = 7×3 table
ytl ybr range ___ ___ __________ 54 60 1×7 double 177 183 1×7 double 300 306 1×7 double 423 429 1×7 double 546 552 1×7 double 669 675 1×7 double 792 798 1×7 double
  7 个评论
Rik
Rik 2021-11-2
What have you tried yourself already? If you try something and post the result it will probably be easy to adapt.

请先登录,再进行评论。

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by