0

The problem I'm facing is the following: I have been given an array of 6x1 which includes 2 NaN that represent 2 unknown values. What I want to do is replace these values with symbolic values (ie. x_1 and x_2 for example). What I don't understand what to do is if i'm given a 12x1 array for example with 10 NaN which I would have to change to X_1, X_2...X_10 in order to use the solver afterwards.

array = [0; 0; NaN; 0; 0; 0; 0; NaN; 0];

Change into:

array = [0; 0; x_1; 0; 0; 0; 0; x_2; 0];

But it has to be a general solution for arrays with different NaN's.

Thanks a lot in advance for your help and time.

4
  • 1
    You can't have a standard array containing numbers and symbolic variables. All entries must have the same data type. You could use a cell array to have each cell contain a different type. But that looks weird. Are you sure you need to mix numeric and symbolic contents? Commented Mar 15, 2017 at 22:43
  • Im given an array with NaN which represent variables, but since i'm programing a solver, the data provided can have 2, 3, 4 NaNs that are N variables. So in short I would like to find the first NaN an replace by x_1, the second by x_2... and then have an array with 0s and variables to run the solver on. Thanks for your time. Commented Mar 15, 2017 at 22:46
  • So you need a symbolic array? If that's the case, add the "symbolic-math" tag so someone with the knowledge can help Commented Mar 15, 2017 at 22:51
  • Yes, adding on what you said I could be given an array with NaNs and numerical values. But just the NaNs need to be changed into symbolic variables. So somehow I would need to have numerical values and symbolic systems in the array if possible, but There might be some other way around to the problem. Commented Mar 15, 2017 at 23:06

1 Answer 1

1

You can use the sym function:

array=sym(array);
array(isnan(array))=sym('x_',[sum(isnan(array)),1])

Explanation: The first line converts the array to a symbolic array. The second replaces the Nan elements with numerically indexed symbolic variables created by the sym function.

Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly what I was looking for. Many thanks for your time.

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.