Stored array won't work when variable is greater then one

My stored array for coordinates does not work when the amount of coordinates on a level is greater then 1. The line of code that doesn't work is:
storedresidentialcoordinates(b,ca,:)=residentialcoordinates;
The full code:
numberoffloors=input('How many floors are there?');
if numberoffloors < 0
message1=msgbox('An invalid number has been entered');
return
end
for a=1:1:numberoffloors
spaces=input('How many spaces are on this floor?');
if spaces < 0
message2=msgbox('An invalid number has been entered');
return
end
numberofspaces(:,a)=spaces;
end
%INSERT MESSAGE BOX%
areares=input('What is the minimum area size for residential rooms?');
areaoff=input('What is the minimum area size for office rooms?');
areaedu=input('What is the minimum area size for education rooms?');
areatoi=input('What is the minimum area size for toilets?');
areastor=input('What is the minimum area size for storage rooms?');
if (areares || areaoff || areaedu || areatoi || areastor) < 0
messagea1=msgbox('An invalid number was entered');
return
end
%INSERT MESSAGE BOX%
ratioresoff=input('What is the ratio of residential to office rooms?');
ratioresedu=input('What is the ratio of residential to education rooms?');
ratiorestoi=input('What is the ratio of residential to toilets?');
ratioresstor=input('What is the ratio of residential to storage rooms?');
ratiooffedu=input('What is the ratio of office to education rooms?');
ratioofftoi=input('What is the ratio of office to toilets?');
ratiooffstor=input('What is the ratio of office to storage rooms?');
ratioedutoi=input('What is the ratio of education to toilet?');
ratioedustor=input('What is the ratio of education to storage rooms?');
ratiotoistor=input('What is the ratio of toilets to storage rooms?');
if (ratioresoff || ratioresedu || ratiorestoi || ratioresstor || ratiooffedu || ratioofftoi || ratiooffstor || ratioedutoi || ratioedustor || ratiotoistor) < 0
messagea2=msgbox('An invalid ratio has been entered');
return
end
for b=1:1:numberoffloors
residential=input('How many residential rooms are there?');
office=input('How many offices are there?');
education=input('How many educational rooms are there?');
toilet=input('How many toilets are there?');
storage=input('How many storage rooms are there?');
figure(b);
if (residential || office || education || toilet || storage) < 0
message3=msgbox('An invalid number has been entered');
return
end
if ratioresoff~=(residential/office) || ratioresedu~=(residential/education) || ratiorestoi~=(residential/toilet) || ratioresstor~=(residential/storage) || ratiooffedu~=(office/education) || ratioofftoi~=(office/toilet) || ratiooffstor~=(office/storage) || ratioedutoi~=(education/toilet) || ratioedustor~=(education/storage) || ratiotoistor~=(toilet/storage)
messagea3=msgbox('The number of rooms entered does not equal the ratios entered');
end
c=[residential office education toilet storage];
total=0;
total=(residential+office+education+toilet+storage);
if total ~= numberofspaces(1,b)
message4=msgbox('The number of spaces does not equal the total of all the individual rooms. Please retry');
return
elseif total == numberofspaces(1,b)
room(b,:)=c;
end
for ca=1:1:residential
xrescoord=input('What is the x-coordinate for this room?');
if xrescoord < 0
message5=msgbox('An invalid number has been entered');
end
yrescoord=input('What is the y-coordinate for this room?');
if yrescoord < 0
message6=msgbox('An invalid number has been entered');
return
end
reswidth=input('What is the width of this residential room?');
if reswidth < 1
message7=msgbox('An invalid number has been entered');
return
end
resheight=input('What is the height of this residential room?');
if resheight < 1
message8=msgbox('An invalid number has been entered');
return
end
if resheight*reswidth < areares
message25=msgbox('Area is less then minimum specification');
return
end
rectangle('position',[xrescoord yrescoord reswidth resheight]);
hold on;
xrcoord(:,ca)=xrescoord
yrcoord(:,ca)=yrescoord
residentialcoordinates=[xrcoord;yrcoord]
storedresidentialcoordinates(b,ca,:)=residentialcoordinates;
end

5 件のコメント

Jan
Jan 2018 年 5 月 3 日
"Does not work" is a weak description of the problem. Please post the error message.
"storedresidentialcoordinates" is an ugly name for a variable. It is not clear to me, what "amount of coordinates on a level is greater then 1" means.
Thomas Sargent
Thomas Sargent 2018 年 5 月 3 日
This is the error message :
Assignment has more non-singleton rhs dimensions than non-singleton subscripts
Error in Coursework (line 120) storedresidentialcoordinates(b,ca,:)=residentialcoordinates;
What i mean by amount of coordinates greater then 1 is when the residential number for the loop is greater then one. i.e the loop 'ca' runs more then once.
Jan
Jan 2018 年 5 月 3 日
The error message means, that storedresidentialcoordinates(b,ca,:) and residentialcoordinates have different sizes. Then the assignment cannot work.
I will not run you code an fight with the ugly bunch of input commands. This is a very inconvenient method to provide values. One typo and you have to restart the code. And if the code runs, there is no chance to check, if there has been a typo during the input. Therefore the result of the code is hardly reproducible.
The debugging is hard also.
Dennis
Dennis 2018 年 5 月 3 日
編集済み: Dennis 2018 年 5 月 3 日
If ca>1 xrcoord and yrcoord contain more than 1 value, so residentialcoordinates is a (ca,2) matrix and i think thats why you get an error message when you try to store it in a single column of storedresidentialcoordinates.
Thomas Sargent
Thomas Sargent 2018 年 5 月 3 日
Yes, essentially i am looking to have a 2 row matrix of x and y coordinates for each iteration of the loop b. How could i do this?

サインインしてコメントする。

回答 (1 件)

Jan
Jan 2018 年 5 月 3 日

0 投票

xrcoord(:,ca)=xrescoord
yrcoord(:,ca)=yrescoord
These lines let xrcoord and yrcoord grow in each iteration. Then
residentialcoordinates=[xrcoord;yrcoord]
Has a different size in each iteration also. In consequence
storedresidentialcoordinates(b,ca,:)=residentialcoordinates;
cannot work in a loop. Maybe you want:
storedresidentialcoordinates(b,ca,:) = [xrescoord; yrescoord];
instead.

カテゴリ

ヘルプ センター および File Exchange で Commercial & Off-Highway Vehicles についてさらに検索

質問済み:

2018 年 5 月 2 日

コメント済み:

2018 年 5 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by