Using for loop with if condition

24 Ansichten (letzte 30 Tage)
G Suresh
G Suresh am 4 Feb. 2026 um 10:31
Kommentiert: Walter Roberson vor etwa 20 Stunden
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.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 4 Feb. 2026 um 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 Kommentare
Walter Roberson
Walter Roberson vor etwa 17 Stunden
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 vor etwa 17 Stunden
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by