The slicing is not complete!

2 次查看(过去 30 天)
Lama Hamadeh
Lama Hamadeh 2021-12-16
评论: Voss 2021-12-17
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 个评论
Voss
Voss 2021-12-17
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.

请先登录,再进行评论。

回答(1 个)

Steven Lord
Steven Lord 2021-12-16
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 个评论
Lama Hamadeh
Lama Hamadeh 2021-12-16
编辑:Lama Hamadeh 2021-12-16
Thanks fro the reply. Why does this difference occur if I define the entries to be equal to1 from the beginning?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by