rescdsk
rescdsk

Reputation: 8915

Set variable in Matlab Dataset array to a single value

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 into 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.

Upvotes: 3

Views: 676

Answers (2)

Rasman
Rasman

Reputation: 5359

it's not elegant but you can also try:

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

Upvotes: 1

abcd
abcd

Reputation: 42235

You can try using ones instead of repmat.

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

Upvotes: 2

Related Questions