The slicing is not complete!

2 views (last 30 days)
Lama Hamadeh
Lama Hamadeh on 16 Dec 2021
Commented: Voss on 17 Dec 2021
Hi all,
I have the following 2D matrix and I want to slice it by taking just the elements on the first column that equal to one. The thing is tht the slicing is turns out t be 50X2 rather than 57x2.
a =[1.0000 0.0399;
1.0000 0.6043;
1.0000 0.4364;
1.0000 0.8750;
1.0000 0.8750;
1.0000 0.8750;
1.0000 0.8750;
1.0000 0.7557;
1.0000 0.8363;
1.0000 0.8123;
1.0000 0.5656;
1.0000 0.3738;
1.0000 0.0285;
1.0000 0.4316;
1.0000 0.3117;
1.0000 0.6250;
1.0000 0.6250;
1.0000 0.6250;
1.0000 0.6250;
1.0000 0.6250;
1.0000 0.9756;
1.0000 0.2671;
1.0000 0.5090;
1.0000 0.4370;
1.0000 0.3156;
1.0000 0.1238;
1.0000 0.7256;
1.0000 0.0171;
1.0000 0.2590;
1.0000 0.8757;
1.0000 0.1870;
1.0000 0.3750;
1.0000 0.3750;
1.0000 0.3750;
1.0000 0.3750;
1.0000 0.3750;
1.0000 0.3750;
1.0000 0.3750;
1.0000 0.9593;
1.0000 0.1816;
1.0000 0.0617;
1.0000 0.0656;
1.0000 0.2419;
1.0000 0.0057;
1.0000 0.0863;
1.0000 0.2919;
1.0000 0.0623;
1.0000 0.1250;
1.0000 0.1250;
1.0000 0.1250;
1.0000 0.1250;
1.0000 0.1250;
1.0000 0.1250;
1.0000 0.1250;
1.0000 0.1250;
1.0000 0.1250;
1.0000 0.9431]; %this is 57x2 matrix
E = a(a(:,1)==1, :) %this should be the same but it turns out to be 50x2!
Any help of why the slicing is throwing away 7 elements would be apprecited.
Thanks.
  1 Comment
Voss
Voss on 17 Dec 2021
Can you show the value of E which is 50-by-2? I get E is 57-by-2 as expected when I run the code.

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 16 Dec 2021
Most likely some of the numbers that are displayed as 1 are stored as a value that is very close to but not down-to-the-last-bit equal to 1. Some of the elements of the following vector will be 0 and some will be very small but not zero.
% Using block comments so the rest of the lines in this answer can be
% executed
%{
difference = a(:, 1) - 1
%}
Compare using a tolerance.
x = 0:0.1:1;
x == 0.3 % none match
ans = 1×11 logical array
0 0 0 0 0 0 0 0 0 0 0
abs(x - 0.3) < eps % element 4 matches
ans = 1×11 logical array
0 0 0 1 0 0 0 0 0 0 0
  1 Comment
Lama Hamadeh
Lama Hamadeh on 16 Dec 2021
Edited: Lama Hamadeh on 16 Dec 2021
Thanks fro the reply. Why does this difference occur if I define the entries to be equal to1 from the beginning?

Sign in to comment.

Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!