Assignment has more non-singleton rhs dimensions than non-singleton subscripts help

1 view (last 30 days)
Hi, I am trying to make something like a table so for every iteration (1:200) a new column is added in 'list_val' but with a variable number of elements in each column. I get the error on the 'list_val(:,it_3)=val_set(o_ue);' line. The following code is embedded in a for loop wiith iteration (it_3=) 1:200. 'list_val', 'val_set' and 'o_ue' are pre-defined and in the first iteration where the error appears the rhs has 3 values.
if length(o_ue)>0
list_val(:,it_3)=val_set(o_ue);
end
  2 Comments
James Tursa
James Tursa on 9 Apr 2015
What do you mean by "... variable number of elements in each column ..."? What are the dimensions of list_val to begin with? What is the size of the val_set(o_ue) result? Are you trying to stuff a variable number of elements in a brand new column with 0 padding on the end?
ajk1
ajk1 on 9 Apr 2015
Hi there, the number of results of val_set(o_ue) is different after each iteration. After the first iteration the size is 3x1 and in the second iteration it will be a different size (nx1 result where n is a integer). I want these values to be stored in list_val, each succeeding iteration being stored in a new column. I haven't assigned a dimension to list_val because I am trying to make the dimensions to be dynamic. I'm trying to store a variable number of elements in a new column with 0 padding. Thanks.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 9 Apr 2015
Edited: Stephen23 on 9 Apr 2015
The trick to including different-length columns into a matrix is to define the subscript indexing, and not just using the colon operator to allocate the whole column:
dat = {[1,2,3],4,[5,6,7,8,9],[]};
out = [];
for k = numel(dat):-1:1
vec = dat{k};
out(1:numel(vec),k) = vec;
end
produces this matrix:
>> out
out =
1 4 5 0
2 0 6 0
3 0 7 0
0 0 8 0
0 0 9 0

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!