Info

This question is closed. Reopen it to edit or answer.

A(I) = B, the number of elements in B and I must be the same

2 views (last 30 days)
Hello,
I know I'm missing something fairly obvious, but I'm getting the error "A(I) = B, the number of elements in B and I must be the same" from a loop in my code:
for j = 1:num_footstrike
myMarkerCycle(j) = yy(start_fs(j):end_fs(j));
end
'num_footstrike' is a row vector, as are 'start_fs', 'end_fs' and 'yy'. Any thoughts?

Answers (2)

Evan
Evan on 2 Jul 2013
Assuming the sections of yy that you're assigning to myMarkerCycle are all the same size, try this:
for j = 1:num_footstrike
myMarkerCycle(j,:) = yy(start_fs(j):end_fs(j));
end
Otherwise, you might need to go some other route, such as using a cell array for myMarkerCycle instead of a matrix.
  2 Comments
Amanda
Amanda on 2 Jul 2013
Ah, I see. The sections of yy are slightly varied in their lengths. So you would recommend using a cell array? I'm not familiar with how those work, but I'll look into it. Thanks!
Evan
Evan on 2 Jul 2013
No problem!
As for the cell array, they have their advantages and disadvantages. They allow you to store multiple datatypes and (more relevant to your case) arrays of variable size in a single array. However, if you end up with very large cell arrays or lots of operations on the cells, you can suffer performance losses and large memory use.
If you for some reason ran into either of these problems, there's one other way to create your array, but it would require padding your rows with zeros to make them all the same length. If you know that none of your data will be zero, this wont be a problem, but if zeros are a possibility, this wouldn't be a very convenient route.

Jan
Jan on 3 Jul 2013
As Evan has pointed out already, this is the method to store the vectors in a cell:
myMarkerCycle = cell(1, num_footstrike)
for j = 1:num_footstrike
myMarkerCycle{j} = yy(start_fs(j):end_fs(j));
end
This is clean and efficient when num_footstrikes is small, e.g. < 10'000. For large arrays it is worth to consider, if the current storage method isn't the most efficient solution already: A dense data vector yy and two index vectors start_fs and end_fs.

Community Treasure Hunt

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

Start Hunting!