MATLAB Faulty Logical Indexing?
2 views (last 30 days)
Show older comments
Using ANSYS APDL, I compiled data into x_disp.xlsx and imported it into matlab using the readmatrix command. The data I have is split up into "e", "Node", and "Displacement" for the first, second, and third column respectively. I only vary "e" for each simulation, so there are always the same number of nodes with different displacement values. My code to split the data is as follows:
filename = 'x_disp.xlsx';
data = readmatrix(filename);
e = -2:0.1:2; % Range of positions tested
data_split{length(e)} = 0;
for ii = 1:length(e)
dist = e(ii);
pos = data(:,1)==e(ii);
fprintf('Solving e=%.1f, successful pos: %i.\n',e(ii),sum(pos))
data_split{ii} = data(pos,:);
end
Running this yields some "e" values being skipped, and data_split has cells that have 0x3 empty cell array. Since the errors are consistent, I need to manually enter in all the data like this:
data_split{8} = data(data(:,1)==-1.3,:);
data_split{12} = data(data(:,1)==-0.9,:);
data_split{13} = data(data(:,1)==-0.8,:);
data_split{15} = data(data(:,1)==-0.6,:);
data_split{17} = data(data(:,1)==-0.4,:);
data_split{18} = data(data(:,1)==-0.3,:);
data_split{19} = data(data(:,1)==-0.2,:);
data_split{20} = data(data(:,1)==-0.1,:);
data_split{21} = data(data(:,1)==6.38378239200000e-16,:);
data_split{22} = data(data(:,1)==0.1,:);
data_split{23} = data(data(:,1)==0.2,:);
data_split{24} = data(data(:,1)==0.3,:);
data_split{25} = data(data(:,1)==0.4,:);
data_split{27} = data(data(:,1)==0.6,:);
data_split{29} = data(data(:,1)==0.8,:);
data_split{30} = data(data(:,1)==0.9,:);
data_split{34} = data(data(:,1)==1.3,:);
The problem lies in here: I cannot index the matrix with a variable, no matter how I format it. I don't know what my error is, but I suspect it is a potential bug.
ii = 1.3;
data(data(:,1)==ii,:); % Won't work, yields 0x3 empty array
data(data(:,1)==1.3,:) % Works. Don't know why or what the difference is, but it works.
The excel sheet I'm using for this example is attached. Thank you in advance for any advice or comments.
0 Comments
Accepted Answer
Stephen23
on 14 May 2020
Edited: Stephen23
on 14 May 2020
"I suspect it is a potential bug"
Nope. The cause is that you are testing for exact equality of binary floating point numbers. That won't work.
When working with binary floating point numbers you must always assume that they have floating point error, e.g. rather than testing for exact equality you should always compare the absolute difference against some tolerance:
abs(A-B)<tol
Select the tolerance values to be suitable for your data magnitude (e.g. 1e-5 perhaps for your data).
Or you should consider using ismembertol, which semi-automatically handles this comparison for you.
If you do any kind of numeric processing using a computer then you need to learn about binary floating point numbers, otherwise you will think that there are bugs in every application and language that you use:
This is worth reading as well:
2 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!