필터 지우기
필터 지우기

Assign random number in column if negative/positive

조회 수: 1 (최근 30일)
Anna Go
Anna Go 2018년 8월 1일
댓글: Anna Go 2018년 8월 1일
Hello everyone,
I have a simple Matlab table with the last column having positive and negative numbers. If the values are negative I want to create an additional table and assign random numbers from 4 to 14 and the rest with "0". If the values are positive I want to assign random numbers from 4 to 14 and the rest "0 " in an additional column. see the attached picture

채택된 답변

KSSV
KSSV 2018년 8월 1일
val = [1 5 -3 4 8 -6]' ;
C1 = zeros(size(val)) ;
idx = val<0 ;
C1(idx) = randsample(4:14,nnz(idx)) ;
T = table(val,C1)
  댓글 수: 3
KSSV
KSSV 2018년 8월 1일
It works very much.....show me the code you tried.
Anna Go
Anna Go 2018년 8월 1일
load energy
whos -file energy
T = table(hour, demand,generation);
T.diffrow = demand - generation;
% insert column with random buy price
T.buydprice = zeros(size(generation)) ;
idx = T.diffrow<0 ;
T.buydprice(idx) = randsample(4:14,nnz(idx)) ;
% insert column with random sell price
T.sellprice = zeros(size(generation)) ;
idx = T.diffrow>0 ;
T.sellprice(idx) = randsample(4:14,nnz(idx)) ;
disp(T)
I attached the .mat file. Thank you

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by