Anyone know why I am recieving this error?

1 次查看(过去 30 天)
z=1;
m=1;
at = 1;
data = rand(7004829, 1);
for k = 2 : at : length(data)
if k ~= (k+1)
index1=k;
y(m,1) = data(index1:z);
z = k;
m = m+1;
end
end

采纳的回答

Image Analyst
Image Analyst 2018-12-28
This is also useless:
if k ~= (k+1)
Can you tell me when k will EVER be equal to k+1? Answer: never. So the condition is always true. That wouldn't cause an error though - it's just bad programming.
Also, id, when you do get around to defining it. Must be a 2-D matrix.
And you might want to preallocate y in advance, though it's not necessary.
And finally, there are no comments. All professional programmers put comments in their code to help them understand later what they did, or to help others understand what they did. I don't really know what you're trying to do. Maybe there is a function or one vectorized line of code to do it, but since there are no comments, I don't know.
  3 个评论
Walter Roberson
Walter Roberson 2018-12-28
image analyst: redefine length as uint8(255). when k reaches uint(255) then k~=k+1 would be false.
... it could happen !
Image Analyst
Image Analyst 2018-12-28
Walter - true. Cem, if you just want to extract matrices that have only the ID number and no other numbers except zero where that number is not the desired number, you can see this example:
m = randi(3, 4, 6) % Sample ID array
m1 = double(ismember(m, 1)) % Get just the 1's
m2 = 2 * double(ismember(m, 2)) % Get just the 2's
m3 = 3 * double(ismember(m, 3)) % Get just the 3's
You'll see
m =
1 1 2 1 3 2
1 1 3 2 3 2
1 2 3 3 2 1
1 3 2 3 3 2
m1 =
1 1 0 1 0 0
1 1 0 0 0 0
1 0 0 0 0 1
1 0 0 0 0 0
m2 =
0 0 2 0 0 2
0 0 0 2 0 2
0 2 0 0 2 0
0 0 2 0 0 2
m3 =
0 0 0 0 3 0
0 0 3 0 3 0
0 0 3 3 0 0
0 3 0 3 3 0
Is that what you're hoping to do?

请先登录,再进行评论。

更多回答(1 个)

madhan ravi
madhan ravi 2018-12-28
编辑:madhan ravi 2018-12-28
  • id is not defined
  • if condition is superfluos though , always k is not equal to k + 1
  • y needs to be preallocated
  • y can be preallocate as a cell to further avoid error ( unable to perform ....due to size)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by