3

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.

2 Answers 2

2

You can try using ones instead of repmat.

myds.Location=4*ones(1,5);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that's nice, but in anything but a trivial case it'll still have to be 4*ones(length(myds),1), which is still kinda ugly.
I understand what you're trying to do, but I don't think it can be done with datasets, although it works perfectly fine with arrays. Your first suggestion is perhaps the simplest. Personally, I would say myds.Location=4*ones(size(myds.Observation)) is quite clear to understand, and gets the job done in a line.
1

it's not elegant but you can also try:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.