# Creating a Function to Divide a Matrix into Three Ranges in Matlab

• MATLAB
• matler
In summary, the conversation is about creating a function in MATLAB that divides a matrix into three ranges and puts the elements into three different 1x3 cells. The proposed solution involves using a for loop and if statements, but it has some limitations and a more efficient solution may involve using the "sort" and "histc" functions.
matler
I'm not very familiar with matlab, trying to learn it little by little by watching some tutorials. However to be able to figure out how it works I need some samples. So, here what I'm trying to do is create a function which takes a matrix, say B matrix, and then divides it into three range;
[0 10), [10 100), and [100 1000)
Then it puts those arrays into 3 different 1x3 cells

Suppose A = [2,4,100,50,40,800]
cell1 = 2,4
cell2 = 40, 50
cell3 = 100, 800

Like that.
Is it possible anyone to create that function, thus I can inspect it to understand the structure.
Suppose not a big deal for someone who's familiar with matlab.

Thanks in advance for any attempt of help

My answer would depend on the scale and variables of the problem, but a simple answer for this specific problem might be:

Code:
[2,4,100,50,40,800];
c=[];d=[];e=[];

for i = 1:numel(A)
if 0 <= A(i) && A(i) < 10
c(numel(c) + 1) = A(i);
elseif 10 <= A(i) && A(i) < 100
d(numel(d) + 1) = A(i);
elseif 100 <= A(i) && A(i) < 1000
e(numel(e) + 1) = A(i);
end
end

This creates three empty vectors (c,d,e) and then loops through all the elements of A using an index i. If an element of A is found to be in the range [0,10), the first element of c is set equal to it, the same with the ranges [10,100) for d and [100,1000) for e. If a second element is found to be in the range [0,10) then the second element of c is set equal to it, this is done using "numel(c) + 1" which finds the number of current elements in c and creates a new element. For example if there are 7 elements in c, numel(c)+1 = 8 and so c(8) is assigned the next value, increasing its size.This code is fairly basic and has two major flaws, first, it is not adaptable for varying number of bins (you must manually set c,d,e), and second, if you have a large number of elements in A, the method of binning, which increases the bins as the loop progresses, is very slow, because when you extend a vector MATLAB must create a new vector, copy, then delete the old one. Extending a large vector many times uses huge memory bandwidth. (see: preallocating)For a larger solution I would try to use the "sort" function, possibly alongside the "histc" function to sort the entire set of elements, then bin them automatically. The MATLAB help should give a good explanation of those two functions.

## 1. What is Matlab and how can it be used?

Matlab is a high-level programming language and interactive environment commonly used in scientific and engineering applications. It allows users to easily manipulate and analyze data, create visualizations, and perform complex calculations.

## 2. How do I input data into Matlab?

Data can be input into Matlab in several ways, including manually entering values into the command window, importing data from a file, or using built-in functions to generate data.

## 3. Can Matlab be used for statistical analysis?

Yes, Matlab has a variety of built-in functions for statistical analysis, such as calculating means and standard deviations, performing hypothesis tests, and creating regression models. Additionally, there are many user-created toolboxes available for more specialized statistical analysis.

## 4. Are there any limitations to using Matlab?

While Matlab is a powerful tool, it does have some limitations. It may not be the best choice for extremely large datasets or complex parallel processing tasks. Additionally, it is a paid software and may not be accessible to everyone.

## 5. Is it necessary to have programming experience to use Matlab?

No, Matlab is designed to be user-friendly and does not require extensive programming experience. However, some basic knowledge of programming concepts such as variables, loops, and functions can be helpful in utilizing Matlab's full capabilities.

Replies
2
Views
3K
Replies
5
Views
1K
Replies
8
Views
1K
Replies
1
Views
2K
Replies
1
Views
3K
Replies
1
Views
2K
Replies
10
Views
2K
Replies
2
Views
11K
Replies
2
Views
3K
Replies
4
Views
3K