Skip to content

YlmzCmlttn

Cemalettin Yılmaz Blog

Menu
  • Home
  • About Me
  • Projects
    • Iot-AR
    • Magnifi-AR
    • Smarthome-IOS
    • Others
  • Categories
    • Articles
    • Augmented Reality
    • Capture The Flag
      • Google CTF
        • 2018
    • Embedded Systems
    • IoT
    • Logisim
    • My Essays
    • Nvidia Jetson
      • Jetson TX1
    • Operating Systems
      • Kali
      • Raspbian
      • Ubuntu
    • Personal
    • Programming
      • Arduino
      • C
      • C#
      • Css
      • Html
      • Js
      • Matlab
      • Node.js
      • Python
      • Swift
      • VHDL
    • Projects
      • Embedded Systems
      • Electric
      • IoT
      • IoT-AR
      • Logisim
      • Magnifi-AR
      • Pose Estimation
    • Raspberry Pi
    • Xilinx
    • Others
Menu

Arrays-matrices: Island area and perimeter | Matlab Examples

Posted on November 26, 2018February 20, 2019 by Yılmaz Cemalettin

Matlab Tutorials | Examples

Practice 7:

Arrays-matrices: Island area and perimeter

The above figure illustrates an example of a 2D grid map of an island, where land is shown in green and the surrounding sea is shown in blue. This grid map can be represented by an n×n matrix where green squares are 1s and blue squares are 0s.

The matrix can be filled manually with INPUT statements if the user wishes to enter the data. An alternative approach could be the use of MAT files.

MAT files are used for saving MATLAB variables for later use, or for transferring them between computers. You can save your variables into a MAT file by giving the save command.

Example:

>> save(‘myvariables.mat’);
The contents of MAT files are retrieved into MATLAB

workspace by making use of the load command. Example: >> fileName= ‘grid1.mat’;
>> load(fileName);

Write a MATLAB program that will do the following jobs:
1. Ask the user if s/he wants to load the grid data from a MAT file.

  1. If the first letter of the answer is y or Y, ask for the name of the file and load it the file contents to memory.
  2. If the first letter of the answer is anything different, ask for each row of the grid one by one. Note that the first row defines the geometry of the n×n square grid. The number of entries in the first row is equal to the number of rows, and how many elements each subsequent row should have. Make sure that the following rows have the correct number of entries, otherwise ask for that row again. Also make sure that all entries are 0s or 1s, otherwise ask for that row again.
  3. Calculate and display the perimeter and the area of the island assuming each side of the square is 1 unit.

 

MAT Files: https://drive.google.com/open?id=14Jy1_fhNS-w8glMEBlukz8y9TwEkT_q4

Solutions:

Matlab
MATLAB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
clc
clear
check = 1;
type = input('Do you want to load grid data from a file? ','s');
if(type == 'Y') || (type == 'y')
    filename = input('Enter MAT file name: ','s');
    load(filename);
else
    counter = 1;
    while(check >0)
        check = 0;
        b = input('Enter row #1 :');
        for i=1:length(b)
            if(b(i)<0) || (b(i)>1) || (b(i)~=ceil(b(i)))
                check = check +1;
            end
        end
        if(check >0)
            fprintf('Entries must consist of 0s or 1s.\n')
        end
    end
    counter = counter +1;
    A(1,:) = b(1,:);
    while(counter<=length(b))
        check = 1;
        fprintf('Enter row #%g :',counter);
        c = input('');
        while(check >0)
            check = 0;
            for i=1:length(c)
                if(c(i)<0) || (c(i)>1) || (c(i)~=ceil(c(i)))
                    check = check +1;
                end
            end
            if length(c) ~= length(b)
                check = check +1;
            end
            if(check >0)
                fprintf('Each row should contain %g entries of 0s and 1s.\n',length(b))
                fprintf('Enter row #%g :',counter);
                c = input('');
            end
        end
        A(counter,:) = c(1,:);
        counter = counter +1;
    end
end
area = 0;
asize = size(A);
arow = asize(1);
acolumn = asize(2);
for i=1:arow
    for j=1:acolumn
        if A(i,j)==1
            area = area + 1;
        end
    end
end
perimeter = area*4;
for i=1:arow
    for j=1:acolumn
        if A(i,j)==1
            if i>1
                if A(i-1,j)==1
                    perimeter=perimeter-1;
                end
            end
            if i<arow
                if A(i+1,j)==1
                    perimeter=perimeter-1;
                end
            end
            if j>1
                if A(i,j-1)==1
                    perimeter=perimeter-1;
                end
            end
            if j<acolumn
                if A(i,j+1)==1
                    perimeter=perimeter-1;
                end
            end
        end
    end
end
fprintf('Island perimeter: %g units\n',perimeter)
fprintf('Island area: %g sq.units \n',area);

 

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

My Motto

“Learn to share, Share to learn”

LinkedIn Badge

Cemalettin Yılmaz

Ads

Archives

Categories

  • Articles (1)
  • Augmented Reality (3)
  • Capture The Flag (23)
    • Google CTF (22)
      • 2018 (13)
      • 2019 (9)
    • PicoCTF (1)
      • 2019 (1)
  • Embedded Systems (3)
  • IoT (3)
  • Logisim (1)
  • My Essays (3)
  • Nvidia Jetson (5)
    • Xavier (5)
  • Operating Systems (24)
    • Kali (3)
    • Raspbian (2)
    • Ubuntu (21)
  • Others (1)
  • Personal (1)
  • Programming (44)
    • Arduino (4)
    • C (10)
    • C# (4)
    • C++ (5)
    • Css (1)
    • CUDA (6)
    • Html (1)
    • Js (2)
    • Libraries (5)
      • OpenCV (3)
      • OpenGL (2)
    • Matlab (14)
    • Node.js (5)
    • Python (6)
    • Swift (3)
  • Programs (4)
    • Tools (4)
  • Projects (21)
    • Books Solutions (8)
    • Electric (2)
    • Embedded Systems (2)
    • Energy Harvesting (1)
    • IoT (2)
    • IoT-AR (1)
    • Logisim (1)
    • Magnifi-AR (3)
    • Pose Estimation (3)
    • Smarthome-Ios (1)
  • Raspberry Pi (3)
  • Uncategorized (2)
  • YZlib (1)

Recent Posts

  • atof stof stod problems with local floating point separator in C/C++
  • Pico CTF 2019 Answers
  • YZlib: Personal C++ Library
  • Drive to target | Google CTF 2019
  • FriendSpaceBookPlusAllAccessRedPremium | Google CTF 2019

Recent Comments

  • AbaShelha on Ghidra Installation on Ubuntu |18.04, 16.04, 14.04
  • Peter on Ghidra Installation on Ubuntu |18.04, 16.04, 14.04
  • Yılmaz Cemalettin on Ghidra Installation on Ubuntu |18.04, 16.04, 14.04
  • Yılmaz Cemalettin on 16-Bit CPU on Logisim
  • Jenny on 16-Bit CPU on Logisim
  • MOON on 16-Bit CPU on Logisim
  • anti on Ghidra Installation on Ubuntu |18.04, 16.04, 14.04
  • hunkerjr on STOP GAN | Google CTF 2019
  • Shaq on 16-Bit CPU on Logisim
  • NURUL AFIQAH MOHD HASBULLAH on 16-Bit CPU on Logisim

Linkedln

© 2022 YlmzCmlttn | Powered by Superbs Personal Blog theme