开发者

Set variable in Matlab Dataset array to a single value

开发者 https://www.devze.com 2023-03-04 06:19 出处:网络
Let\'s say I have a dataset array (from the statistics toolbox): >> myds myds = ObservationSheepCount

Let's say I have a dataset array (from the statistics toolbox):

>> myds
myds = 
    Observation    SheepCount
    1              88        
    2               2        
    3              14        
    4              12        
    5              40

I'm putting together data from various sources, so I'd like to set 'Location' to be 4 in all of these observations, before I vertcat this dataset together with others. In a normal matrix, you'd say myds(:, 3) = 4, which would broadcast the 4 int开发者_C百科o all of the spaces in the matrix.

Is there a way to do that on a dataset without using repmat?

Things I've tried that don't work:

myds(:, 'Location') = 4
myds(:).Location = 4
myds.Location(:) = 4
myds.Location = 4

Things that work:

myds.Location = 4; myds.Location(:) = 4; % have to run both
myds.Location = repmat(4, length(myds), 1);

So, do I have to get over my aversion to repmat? Thanks.

edit: I guess what I actually want is to avoid specifying the dimensions of the array of 4's.


You can try using ones instead of repmat.

myds.Location=4*ones(1,5);


it's not elegant but you can also try:

myds.Location= myds.Observation*0 + 4;
0

精彩评论

暂无评论...
验证码 换一张
取 消