MATLAB Answers

Anyone know why I am recieving this error?

1 view (last 30 days)
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 Comment

Walter Roberson
Walter Roberson on 28 Dec 2018
Please do not close questions that have an Answer.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 28 Dec 2018
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 Comments

Cem SARIKAYA
Cem SARIKAYA on 28 Dec 2018
yeah i am sorry
i fix like that
z=1;
m=1;
at = 1;
data = rand(7004829, 1);
for k = 1 : at : length(data);
if id(k,1) ~= id((k+1),1)
index1=k;
y(m,1) = data(index1:z);
z = k;
m = m+1;
end
end
but it is not working like that too i have a matris which have id numbers and i need to separate according to id numbers
Walter Roberson
Walter Roberson on 28 Dec 2018
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 on 28 Dec 2018
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?

Sign in to comment.

More Answers (1)

madhan ravi
madhan ravi on 28 Dec 2018
Edited: madhan ravi on 28 Dec 2018
  • 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 Comments

Cem SARIKAYA
Cem SARIKAYA on 28 Dec 2018
is matlab work concurrent assignment? i need this code not concurrent assignment like c++, and i fix id

Sign in to comment.

Sign in to answer this question.

Tags