Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set variable in Matlab Dataset array to a single value

Tags:

syntax

matlab

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.

like image 517
rescdsk Avatar asked Feb 27 '26 12:02

rescdsk


1 Answers

You can try using ones instead of repmat.

myds.Location=4*ones(1,5);
like image 80
abcd Avatar answered Mar 02 '26 02:03

abcd