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.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 4 de Feb. de 2026 a las 11:37
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 comentarios

G Suresh
G Suresh el 5 de Feb. de 2026 a las 5:11
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
Walter Roberson
Walter Roberson el 5 de Feb. de 2026 a las 19:59
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);
Walter Roberson
Walter Roberson el 5 de Feb. de 2026 a las 20:02
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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 4 de Feb. de 2026 a las 10:31

Comentada:

el 5 de Feb. de 2026 a las 20:02

Community Treasure Hunt

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

Start Hunting!

Translated by