0

I have a double column vector as follows:

[1;NaN;NaN;2;NaN;NaN;3;NaN;NaN;] 

What I'd like to do is create a string variable name on the NaN elements that follow the numbers. For example, I'd like my final vector to look like this:

[1;wd1;wd1;2;wd2;wd2;3;wd3;wd3] 

Can anyone help out? Thanks!

2
  • Your initial vector is of type int[] and NaN is compatible with that. A random string is not. So there are follow up questions on what you ask: Is it ok to change that type? For example to use cells instead? Is there any significance on the "string variable name" or all you want to do is replace NaNs with specific strings? Finally, is this a pattern that between the number 1 and the next number 2 all NaNs should be converted to wd1? Commented Jul 24, 2019 at 21:44
  • I'm ok with changing the type to cells. The significance of the names is dependent on the pattern. Per the aray, we have numerals 1,2,3 with NaNs in between. The objective is to be able to convert all the NaNs in between to the corresponding number, so it would be [1,wd1,wd1,2,wd2,wd2,3,wd3,wd3]. Commented Jul 24, 2019 at 23:02

1 Answer 1

1

From what I understand, you want something like this:

b = num2cell(a); numberPostfix = 0; %in case the first element is NaN for i = 1:numel(b) if( isnan(b{i}) ) b{i} = ['wd' num2str(numberPostfix)]; else numberPostfix = b{i}; end end 

For input vector

a = (1:20)'; a(3:4) = NaN; a(12:18) = NaN; 

this will result in

b = {1;2;'wd2';'wd2';5;6;7;8;9;10;11;'wd11';'wd11';'wd11';'wd11';'wd11';'wd11';'wd11';19;20} 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.