MATLAB Answers


Error finding matrix indices where elements obey a condition

Asked by ferda sonmez on 22 Mar 2019
Latest activity Commented on by Luna
on 28 Mar 2019
I have a (30,1) matrix A filled with both positive and negative double values with high precisions.
I do the B= A>0 expecting a resulting matrix filled with 0s and 1s based on the value of original matrix such as
However, when I display I get a matrix like below
0.0 < -60.80089912054130723383673691361028456512560544254638307756720395046146077078219605027697980403900146484375
0.0 < -40.47441497086561319883182687840919573785507473004455923328153712191512791918057700968347489833831787109375
0.0 < 11.9150901590727927897158716535914255438713276913571208738565018227240077663964257226325571537017822265625
0.0 < 50.24337655630030618369864852262179095255359883940103235308691394245261818696235422976315021514892578125
0.0 < -12.788094842081277874683606597932106962408104528383932295293478643982698628178695798851549625396728515625
0.0 < -19.7994345791724984686511623375698392780606832842132285447736268595153585891921466100029647350311279296875
0.0 < 44.20960440529531749044620672799080999950637846265719961065220598196712220584458918892778456211090087890625
0.0 < -38.4752715808701180299125340913012275096601820744906678455217984013980725421788520179688930511474609375
0.0 < -57.1261686699021779161260124218737353314343618323505877827184033790341999292650143615901470184326171875
0.0 < -15.50675798085333259859508957067109961395543474125240585269404109021451620975540208746679127216339111328125 0
Later when use matrix B in a matrix multiplication I get a lot of NaN values. Where am I doing wrong? I will be very appreciated if I can find a solution dfor this . Cause I am struggling due to this issue for a few days..
Best Regards,


As far as I understand your data already symbolic in your workspace. You should convert it into double array see my answer below.
You can also convert it before saving, when you load you will get the double array instead of symbolic array.
A = double(input_of_hidden_layer2)
load('SavedMatrix1.mat') % you will see A 30x1 double in your workspace.
When you use vpa() or vpasolve() the class of the variable becomes symbolic.
Ah now I understand, thx :)

Sign in to comment.




2 Answers

Answer by Luna
on 22 Mar 2019
 Accepted Answer

Hi Ferda,
Please see my comments below:
% first convert it to double array:
A = double(input_of_hidden_layer2);
% indexing your conditions:
B = A > 0;
% get the values which are greater than zero from that index:


Sign in to comment.

Answer by Walter Roberson
on 23 Mar 2019

B = isAlways(A>0);
will give you a logical result. Or you could
B = logical(A>0);

  1 Comment

Sign in to comment.