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
댓글 수: 1
채택된 답변
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
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
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
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)
댓글 수: 2
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!