Using for loop with if condition

Respectd Team,
I am looking solution for genration of data load by text.
My problem in simple manner, We have 14 floor building with 20 rooms in each floor, I have to write a code for once the person is enter into the room 1st floor 101 and remaining values in corresponding top floors should be zer (ex :201,301....1401) and if the person enter into 2room no 415 (4 floor & no 15) than correpsonding values in 115,215,315,515....1415 should be zero.

 Accepted Answer

MaxFloors = 14;
MaxRooms = 20;
BuildingData = zeros(MaxFloors, MaxRooms);
FloorRoomData = [1 1; 4 15];
for Row = 1 : height(FloorRoomData);
FloorIndex = FloorRoomData(Row, 1);
RoomIndex = FloorRoomData(Row, 2);
BuildingData(:, RoomIndex) = 0;
BuildingData(FloorIndex, RoomIndex) = 1;
end
BuildingData
BuildingData = 14×20
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

3 Comments

Thnak you for your quick reponse.
I need more clarification, if all the floors should be verified with value along with condition.
My problem as follows, I have text data need to load and print as required format. Out put is not printing as per our requirement
M=load('HST.txt')
l=32;% number of layers
z=61; % number of zones
n=l*z;
fs1=fopen('section.txt','w');
fprintf(fs1,'/prep7\n');
for i=1:z
for j=1:l
t(i,j)=M(i,j)
end
end
B=t(:)'; % transpose and reshape matrix
k=1;
j=1;
ln=0;
for i=1:n
if B(1,i)>0
fprintf(fs1,'sect%d, shell,\n',j);
%%
if ln==k;
for k=1:ln %% number of layer
fprintf(fs1,'secdata,0, %f, %f\n',M(i,k+32),M(i,k+64));
end
fprintf(fs1,'secdata %f, %f, %f\n',t(i,k),M(i,k+32),M(i,k+64));
else
for k=ln:32 %% number of layer
fprintf(fs1,'secdata,0, %f, %f\n',M(i,k+32),M(i,k+64));
end
j=j+1;
k=k+62;
end
end
end
for i=1:z
for j=1:l
t(i,j)=M(i,j)
end
end
That can be more easily done as
t = M(1:z, 1:l);
You have presented code with few useful comments. Under the circumstances, we must understand that the defined requirements of the code are identical to the current implementation of the code, that the purpose of the code is to compute exactly whatever it presently calculates. Therefore, you are incorrect that the code is not printing as per your requirement, since your requirement is defined by exactly what it prints.

Sign in to comment.

More Answers (0)

Asked:

on 4 Feb 2026 at 10:31

Commented:

on 5 Feb 2026 at 20:02

Community Treasure Hunt

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

Start Hunting!