3

I am wondering if there is some way, how to multiple assign values to different variables according logical vector.

For example:

I have variables a, b, c and logical vector l=[1 0 1] and vector with values v but just for a and c. Vector v is changing its dimension, but everytime, it has the same size as the number of true in l.

I would like to assign just new values for a and c but b must stay unchanged.

Any ideas? Maybe there is very trivial way but I didn't figure it out.

Thanks a lot.

2
  • @JacubJon just for curiosity, if I understood your question right. Could you please check if my solution would also work? Because if it's an invalid solution I'd rather delete it. Commented Dec 17, 2013 at 9:37
  • @thewaywewalk, I didn't try your solution but I think we don't understand each other. In case I change logical vector 'l' to '[0 1 0]' means that I would like to assign new value just for variable 'b' and that means vector 'v' changed its dimension to 1 because it contains just new value for 'b'.. Is it clear now. Thanks Commented Dec 23, 2013 at 10:22

1 Answer 1

1

I think your problem is, that you stored structured data in an unstructured way. You assume a b c to have a natural order, which is pretty obvious but not represented in your code.

Replacing a b c with a vector x makes it a really easy task.

x(l)=v(l);

Assuming you want to keep your variable names, the simplest possibility I know would be to write a function:

function varargout=update(l,v,varargin)
varargout=varargin;
l=logical(l);
varargout{l}=v(l);
end

Usage would be:

[a,b,c]=update(l,v,a,b,c)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, for sure, I can do that like this, but my variables a, b, c comes from different sources (structures) and I would like to keep continuity and make it in easiest way.

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.