10 मिनट में MATLAB सीखें
MATLAB (Matrix Laboratory) एक उच्च-स्तरीय प्रोग्रामिंग भाषा और वातावरण है जो संख्यात्मक कंप्यूटिंग, एल्गोरिदम विकास और डेटा विज़ुअलाइज़ेशन के लिए डिज़ाइन किया गया है। 1970 के दशक के अंत में विकसित होने के बाद, MATLAB इंजीनियरिंग, विज्ञान और गणित में एक आवश्यक उपकरण बन गया है।
यह ट्यूटोरियल MATLAB की मूल बातें कवर करता है और तुम्हें इस शक्तिशाली कंप्यूटिंग वातावरण के साथ शुरू करने में मदद करता है।
1. तुम्हारा पहला MATLAB प्रोग्राम
hello.m नामक एक स्क्रिप्ट फ़ाइल बनाओ या MATLAB कमांड विंडो में सीधे कमांड टाइप करो।
disp('Hello, World!')
या fprintf फंक्शन का उपयोग करके:
fprintf('Hello, World!\n')
आउटपुट होगा:
Hello, World!
disp() फंक्शन सामग्री को सीधे दिखाता है, जबकि fprintf() अधिक फॉर्मेटिंग नियंत्रण प्रदान करता है, जो C के printf के समान है।
2. बुनियादी सिंटैक्स
MATLAB का अपना सिंटैक्स नियम है जो अधिकांश सामान्य-उद्देश्य प्रोग्रामिंग भाषाओं से भिन्न है। साफ-सुथरा, बनाए रखने में आसान कोड लिखने के लिए इन मूल बातों को समझना आवश्यक है।
2.1 टिप्पणियाँ
एकल-पंक्ति टिप्पणियाँ प्रतिशत चिह्न % से शुरू होती हैं:
% This is a comment
x = 10; % This assigns 10 to x
बहु-पंक्ति टिप्पणियाँ %{ और %} का उपयोग करती हैं:
%{
This is a multi-line comment.
It can span multiple lines.
%}
2.2 अर्धविराम
अर्धविराम ; आउटपुट प्रदर्शन को दबा देता है। इसके बिना, MATLAB परिणाम दिखाता है:
x = 5 % Displays: x = 5
y = 10; % No output, but y is assigned
2.3 केस संवेदनशीलता
MATLAB केस-संवेदनशील है। A और a अलग-अलग चर हैं:
A = 5;
a = 10;
disp(A) % Output: 5
disp(a) % Output: 10
2.4 चर नामकरण
चर के नाम एक अक्षर से शुरू होने चाहिए, उसके बाद अक्षर, संख्याएँ या अंडरस्कोर आ सकते हैं:
valid_name = 1;
another_valid_name_123 = 2;
% 123invalid = 3; % Error: cannot start with number
2.5 बुनियादी संक्रियाएँ
MATLAB मैट्रिक्स संक्रियाओं में उत्कृष्ट है। एक मैट्रिक्स बनाना आसान है:
A = [1 2 3; 4 5 6; 7 8 9] % 3x3 matrix
आउटपुट:
A =
1 2 3
4 5 6
7 8 9
3. चर और डेटा प्रकार
MATLAB डायनामिक टाइपिंग का उपयोग करता है। चर असाइन करने पर बनाए जाते हैं और उनका प्रकार असाइन किए गए मान से निर्धारित होता है।
3.1 संख्यात्मक प्रकार
MATLAB डिफ़ॉल्ट रूप से सभी संख्याओं को डबल-प्रिसिजन फ्लोटिंग पॉइंट के रूप में संग्रहीत करता है:
% Integer
int_num = 42;
% Float
float_num = 3.14159;
% Scientific notation
sci_num = 2.5e-3; % 0.0025
3.2 स्ट्रिंग्स
स्ट्रिंग चर एकल उद्धरणों का उपयोग करते हैं:
str = 'Hello, MATLAB';
str2 = "Hello, MATLAB"; % String array (R2016b+)
स्ट्रिंग संयोजन:
str1 = 'Hello';
str2 = 'World';
combined = [str1 ', ' str2]; % 'Hello, World'
3.3 लॉजिकल (बूलियन)
लॉजिकल मान true या false होते हैं:
flag = true;
result = false;
% Logical operations
a = true;
b = false;
and_result = a && b; % false
or_result = a || b; % true
not_result = ~a; % false
3.4 कैरेक्टर एरे बनाम स्ट्रिंग्स
MATLAB में कैरेक्टर एरे और स्ट्रिंग ऑब्जेक्ट दोनों होते हैं:
% Character array (older style)
char_arr = 'Hello';
% String object (modern style, R2016b+)
str_obj = "Hello";
% String objects are easier to work with
name = "Alice";
greeting = "Hello, " + name; % Works naturally
4. डेटा संरचनाएँ
MATLAB विभिन्न उपयोग मामलों के लिए कई डेटा संरचनाएँ प्रदान करता है।
4.1 वेक्टर
एक वेक्टर एक-आयामी एरे है:
% Row vector
row_vec = [1 2 3 4 5];
% Column vector
col_vec = [1; 2; 3; 4; 5];
% Using colon operator
range_vec = 1:5; % [1 2 3 4 5]
step_vec = 0:2:10; % [0 2 4 6 8 10]
% linspace for evenly spaced values
lin_vec = linspace(0, 10, 5); % [0 2.5 5 7.5 10]
4.2 मैट्रिक्स
मैट्रिक्स MATLAB की नींव हैं:
% Direct matrix creation
A = [1 2 3; 4 5 6; 7 8 9];
% Accessing elements
element = A(2, 3); % Returns 6 (row 2, column 3)
% Matrix operations
B = A'; % Transpose
C = A * B; % Matrix multiplication
D = A .* B; % Element-wise multiplication
4.3 सेल एरे
सेल एरे विभिन्न प्रकार के डेटा रख सकते हैं:
% Create a cell array
cell_arr = {1, 'hello', [1 2 3], true};
% Access cell contents using curly braces
data = cell_arr{2}; % Returns 'hello'
% Access cell using parentheses
sub_cell = cell_arr(1:2); % Returns {1, 'hello'}
4.4 संरचनाएँ
संरचनाएँ नामित फ़ील्ड वाले शब्दकोशों के समान हैं:
% Create a structure
student.name = 'John';
student.age = 20;
student.major = 'Engineering';
% Access fields
disp(student.name); % Output: John
% Array of structures
students(1).name = 'Alice';
students(1).age = 21;
students(2).name = 'Bob';
students(2).age = 22;
4.5 टेबल
टेबल टैब्यूलर डेटा के लिए उपयुक्त हैं:
% Create a table
Age = [25; 30; 35];
Name = {'Alice'; 'Bob'; 'Charlie'};
Salary = [50000; 60000; 70000];
T = table(Name, Age, Salary);
% Access data
disp(T.Age);
disp(T.Name{1});
5. ऑपरेटर
MATLAB अंकगणित, तुलना और लॉजिकल संक्रियाओं के लिए विभिन्न ऑपरेटर प्रदान करता है।
5.1 अंकगणितीय ऑपरेटर
a = 10;
b = 3;
sum = a + b; % 13
diff = a - b; % 7
prod = a * b; % 30
quot = a / b; % 3.3333
int_div = floor(a/b); % 3
mod = mod(a, b); % 1 (remainder)
pow = a ^ b; % 1000
5.2 एलीमेंट-वाइज ऑपरेटर
एलीमेंट-वाइज ऑपरेटर संबंधित एलीमेंट पर काम करते हैं:
A = [1 2 3];
B = [4 5 6];
C = A .* B; % Element-wise: [4 10 18]
D = A.^2; % Element-wise square: [1 4 9]
5.3 तुलना ऑपरेटर
x = 5;
y = 10;
eq = (x == y); % false
neq = (x ~= y); % true
gt = (x > y); % false
lt = (x < y); % true
ge = (x >= y); % false
le = (x <= y); % true
5.4 लॉजिकल ऑपरेटर
a = true;
b = false;
and_op = a & b; % false (element-wise AND)
or_op = a | b; % true (element-wise OR)
not_op = ~a; % false
and_short = a && b; % false (short-circuit AND)
or_short = a || b; % true (short-circuit OR)
6. नियंत्रण प्रवाह
MATLAB मानक नियंत्रण प्रवाह संरचनाएँ प्रदान करता है, लेकिन Python जैसी भाषाओं से अलग सिंटैक्स के साथ।
6.1 if-elseif-else
score = 85;
if score >= 90
grade = 'A';
elseif score >= 80
grade = 'B';
elseif score >= 70
grade = 'C';
else
grade = 'F';
end
disp(grade) % Output: B
6.2 switch
switch स्टेटमेंट एकल एक्सप्रेशन को कई केसों के विरुद्ध तुलना करता है:
day = 'Monday';
switch day
case {'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'}
disp('Weekday');
case {'Saturday', 'Sunday'}
disp('Weekend');
otherwise
disp('Invalid day');
end
6.3 for लूप
for लूप एक रेंज या एरे पर इटरेट करता है:
% Iterating over a range
for i = 1:5
disp(i);
end
% Iterating over an array
fruits = {'apple', 'banana', 'orange'};
for fruit = fruits
disp(fruit{1});
end
% Nested loops for matrix operations
A = [1 2; 3 4];
B = zeros(2, 2);
for i = 1:2
for j = 1:2
B(i, j) = A(i, j) * 2;
end
end
6.4 while लूप
count = 0;
while count < 5
disp(count);
count = count + 1;
end
6.5 लूप नियंत्रण
break स्टेटमेंट लूप से बाहर निकलता है, और continue अगले इटरेशन पर छोड़ देता है:
% Using break
for i = 1:10
if i == 5
break;
end
disp(i);
end
% Output: 1 2 3 4
% Using continue
for i = 1:5
if mod(i, 2) == 0
continue; % Skip even numbers
end
disp(i);
end
% Output: 1 3 5
7. इनपुट और आउटपुट
7.1 यूज़र इनपुट
यूज़र इनपुट प्राप्त करने के लिए input() फंक्शन का उपयोग करो:
% Get numeric input
num = input('Enter a number: ');
% Get string input
name = input('Enter your name: ', 's');
% Get expression input (evaluates the input)
expr = input('Enter an expression: ');
7.2 आउटपुट प्रदर्शित करना
कई फंक्शन आउटपुट दिखाते हैं:
% disp - simple display
disp('Hello');
disp([1 2 3]);
% fprintf - formatted output
name = 'Alice';
age = 25;
fprintf('Name: %s, Age: %d\n', name, age);
% sprintf - create formatted string
str = sprintf('Value: %.2f', 3.14159);
disp(str);
7.3 फॉर्मेट स्पेसिफायर
MATLAB में सामान्य फॉर्मेट स्पेसिफायर:
% %s - string
% %d - integer
% %f - floating point
% %.2f - floating point with 2 decimal places
% %e - scientific notation
fprintf('%d %.2f %e\n', 42, 3.14159, 1000)
% Output: 42 3.14 1.000000e+03
8. फंक्शन
MATLAB में फंक्शन आमतौर पर अलग फ़ाइलों में परिभाषित होते हैं, लेकिन एनोनिमस फंक्शन इनलाइन फंक्शन क्रिएशन प्रदान करते हैं।
8.1 एनोनिमस फंक्शन
एनोनिमस फंक्शन अलग फ़ाइलों के बिना सरल फंक्शन बनाते हैं:
% Single input
square = @(x) x^2;
disp(square(5)); % Output: 25
% Multiple inputs
add = @(x, y) x + y;
disp(add(3, 4)); % Output: 7
% Multiple expressions
hypot = @(x, y) sqrt(x^2 + y^2);
disp(hypot(3, 4)); % Output: 5
8.2 फंक्शन फ़ाइलें
myfunc.m नामक एक फ़ाइल बनाओ:
function y = myfunc(x)
y = x^2 + 1;
end
फंक्शन को कॉल करो:
result = myfunc(5); % Output: 26
8.3 एकाधिक आउटपुट वाले फंक्शन
function [sum, prod] = calc(x, y)
sum = x + y;
prod = x * y;
end
एकाधिक आउटपुट के साथ कॉल करो:
[s, p] = calc(3, 4);
disp(s); % 7
disp(p); % 12
8.4 वेरिएबल आर्ग्यूमेंट्स
वेरिएबल आर्ग्यूमेंट्स के लिए varargin और varargout का उपयोग करो:
function result = sum_all(varargin)
result = 0;
for i = 1:length(varargin)
result = result + varargin{i};
end
end
% Call with any number of arguments
total = sum_all(1, 2, 3, 4, 5); % Output: 15
9. स्क्रिप्ट फ़ाइलें
स्क्रिप्ट .m फ़ाइलें हैं जो MATLAB कमांड का क्रम शामिल करती हैं। ये वर्कस्पेस में डेटा पर काम करती हैं:
% save as myscript.m
% Calculate statistics for a dataset
data = [1 2 3 4 5 6 7 8 9 10];
mean_val = mean(data);
std_val = std(data);
max_val = max(data);
min_val = min(data);
fprintf('Mean: %.2f\n', mean_val);
fprintf('Std: %.2f\n', std_val);
fprintf('Max: %d\n', max_val);
fprintf('Min: %d\n', min_val);
10. त्रुटि हैंडलिंग
त्रुटि हैंडलिंग के लिए try-catch का उपयोग करो:
try
result = risky_operation();
catch ME
fprintf('Error: %s\n', ME.message);
% Handle the error
result = 0;
end
त्रुटियाँ उठाने के लिए error() का उपयोग करके:
function result = divide(a, b)
if b == 0
error('Division by zero is not allowed');
end
result = a / b;
end
11. फ़ाइल संक्रियाएँ
11.1 डेटा सहेजना और लोड करना
% Save variables to file
x = [1 2 3];
y = 'hello';
save('data.mat', 'x', 'y');
% Load variables from file
load('data.mat');
% Save to text file
writematrix(x, 'data.txt');
% Read from text file
data = readmatrix('data.txt');
11.2 टेक्स्ट फ़ाइल संक्रियाएँ
% Write to text file
fid = fopen('output.txt', 'w');
fprintf(fid, 'Line 1\n');
fprintf(fid, 'Line 2\n');
fclose(fid);
% Read from text file
fid = fopen('output.txt', 'r');
while ~feof(fid)
line = fgetl(fid);
if ischar(line)
disp(line);
end
end
fclose(fid);
स्ट्रक्चर्ड रीडिंग के लिए textscan का उपयोग करके:
fid = fopen('data.txt', 'r');
format = '%s %d %f';
C = textscan(fid, format);
fclose(fid);
name = C{1};
age = C{2};
score = C{3};
12. प्लॉटिंग
MATLAB की प्लॉटिंग क्षमताएँ इसकी सबसे मजबूत विशेषताओं में से एक हैं।
12.1 बेसिक 2D प्लॉटिंग
x = 0:0.1:2*pi;
y = sin(x);
plot(x, y);
xlabel('x');
ylabel('sin(x)');
title('Sine Wave');
grid on;
12.2 एकाधिक प्लॉट
x = 0:0.1:2*pi;
% Subplots
subplot(2, 1, 1);
plot(x, sin(x));
title('Sine');
subplot(2, 1, 2);
plot(x, cos(x));
title('Cosine');
12.3 प्लॉट कस्टमाइज़ेशन
x = 0:0.1:10;
y1 = x;
y2 = x.^2;
plot(x, y1, 'b-', 'LineWidth', 2); % Blue solid line
hold on;
plot(x, y2, 'r--', 'LineWidth', 2); % Red dashed line
hold off;
xlabel('X');
ylabel('Y');
legend('Linear', 'Quadratic');
title('Linear vs Quadratic');
grid on;
12.4 अन्य प्लॉट प्रकार
% Bar chart
bar([1 2 3 4], [10 20 15 25]);
% Histogram
data = randn(1000, 1);
histogram(data, 30);
% Scatter plot
x = rand(100, 1);
y = 2*x + randn(100, 1)*0.1;
scatter(x, y);
