MATLAB Answers

how to insert 1 in place of inf in a given array

4 views (last 30 days)
anshuman mishra
anshuman mishra on 28 Jul 2019
Commented: Stephen Cobeldick on 30 Jul 2019
given array :
Inf 4 2 2 4 Inf
i want my final output satisfy two conditions:
1. insert value 1 wherever Inf is encountered
2.insert value 1 wherever 2 or any number is encountered multiple times consequetively.
final output : 1 0 1 0 0 1
Edit: Thanks a lot,that worked like hot knife on butter :)
i've got another issue:I want to append 1 to start of every array
Ex-: a= [0 1 0 01]
my desired output= [1 0 1 0 0 1]

  0 Comments

Sign in to comment.

Accepted Answer

Rik
Rik on 28 Jul 2019
Edited: Rik on 28 Jul 2019
You can use the diff function and isinf to make this array.
final= [false diff(array)==0] | isinf(array) ;

  7 Comments

Show 4 older comments
anshuman mishra
anshuman mishra on 29 Jul 2019
thank you . one more help : how to count no. of 1s in the final array ?
Rik
Rik on 29 Jul 2019
The sum function can be used as a shorthand.

Sign in to comment.

More Answers (0)

Sign in to answer this question.


Translated by