For loop gives error: Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.

1 view (last 30 days)
Having problems with making a For loop for example below. Desired outcome: skylineMat = 2x1 cell.
Example (without for loop: works):
skylineMat = true(90,360);
Q1 = inpolygon( repmat(azimV,length(altV),1) , repmat(altV',1,length(azimV)) , skyline_raw{1}(:,1) , skyline_raw{1}(:,2) );
skylineMat1 = skylineMat & ~ Q1;
Q2 = inpolygon( repmat(azimV,length(altV),1) , repmat(altV',1,length(azimV)) , skyline_raw{2}(:,1) , skyline_raw{2}(:,2) );
skylineMat2 = skylineMat & ~ Q2;
skylineMat = { {skylineMat1(:,:) } ; { skylineMat2(:,:) } }
Example (with for loop: gives me the error in the title):
skylineMat = true(90,360);
shapes_num = 2;
for s=1:shapes_num
Q(s) = inpolygon( repmat(azimV,length(altV),1) , repmat(altV',1,length(azimV)) , skyline_raw{s}(:,1) , skyline_raw{s}(:,2) );
skylineMat(s) = skylineMat & ~ Q(s);
end

Answers (1)

KSSV
KSSV on 1 Mar 2021
This error occurs, when you try to save more number of elements than you initialized.
Example:
A = rand(10,5) ; % initialize array
A(1,:) = rand(1,5) ; % no error
A(2,:) = rand(1,7) ; % error, you have to save 1x5 but you tried to save 1x7, so error.
Use debug options and try to check the dimensions of RHS and then initialize LHS and then save.
If the dimensions are not known, try to save them into a cell.
Example:
A = cell(1,5) ;
A{1} = rand(1,5) ;
A{2} = rand(1,10) ;

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!