[go: up one dir, main page]

0% found this document useful (0 votes)
20 views9 pages

Miron MATLAB5

Basic matlab exercise

Uploaded by

Madhur Miradwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views9 pages

Miron MATLAB5

Basic matlab exercise

Uploaded by

Madhur Miradwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

PART 1: MATLAB Date of Performance: 05-08-2025

Exercise 5
Cell Array and Character Array

1. Write a program to create a cell array of size 10, which contains Sr no. , colors and
each person is allotted to the color.

Logic:
- Create two lists: one for colors, one persons.
- Initialize an empty 10×3 cell array structure.
- Fill each row with Sr No., color, and person.

- Print each row using formatted fprintf function.

Code:

Output:

DYSBB22U0BID013 37
PART 1: MATLAB Date of Performance: 05-08-2025

2. Write a program to take the cell array from above example to perform following
actions.
i. Access a cells of cell array using () and {} brackets. (One cell of color and one
of a person)
ii. Delete a cell. (One of color and one of person)
iii. Replace deleted cells with its respective data.

Logic:

- Create 10×3 cell array with serials, colors, persons.


- Access color using () and person using {}.
- Assign empty [] using {} to "delete" content.
- Display the array after deleting those two cells.
- Replace deleted values by assigning original data back.

Code:

DYSBB22U0BID013 38
PART 1: MATLAB Date of Performance: 05-08-2025

Output:

3.
3.
3.
3.
3.
3.
3.
3.
3.
3.
3.
3.
3.
3.
3.
3.
3.
3.
3.
Write a program to demonstrate the concatenation of 4 cell arrays vertically and
horizontally.

Logic:

- Define four cell arrays with two elements each.


- Use square brackets for both concatenation types.
- Horizontal concatenation uses commas or space.
- Vertical concatenation uses semicolons between arrays.
- Display results using variable names directly in script.

Code:

DYSBB22U0BID013 39
PART 1: MATLAB Date of Performance: 05-08-2025

Output:

DYSBB22U0BID013 40
PART 1: MATLAB Date of Performance: 05-08-2025

4. Write a program to demonstrate intersect function:


i. Create a two cell arrays consists of character vectors of size 1X8. (Display
both cases one with trailing spaces and one without the trailing spaces. Use
strings instead of number).

Logic:

- Create cell array with trailing spaces in strings.


- Create second cell array without trailing spaces in strings.
- Remove trailing spaces using strtrim on first array.
- Use intersect to find common elements between arrays.
- Display both arrays and their intersection results clearly.

Code:

Output:

DYSBB22U0BID013 41
PART 1: MATLAB Date of Performance: 05-08-2025

ii. Create one character vector and one cell array of size 1X8. (Use strings
instead of number).

Logic:
- Convert character vector into a cell array of strings.
- Create a cell array with multiple string elements.
- Use intersect to find common strings in both arrays.
- Display both the character vector (as cell) and array.
- Print the intersection showing common elements between them.

Code:

Output:

DYSBB22U0BID013 42
PART 1: MATLAB Date of Performance: 05-08-2025

5. Write a program to demonstrate the concatenation of rectangular character


array vertically. (Display both the cases using functions).

Logic:

- Define two rectangular character arrays with equal column width.


- Vertically concatenate arrays using square brackets and semicolon.
- Display result directly using disp() in the main script.
- Create a function that accepts two character arrays.
- Inside function, concatenate and display the combined result.

Code:

DYSBB22U0BID013 43
PART 1: MATLAB Date of Performance: 05-08-2025

Output:

6. Write a program to
demonstrate the
concatenation of
rectangular
character array horizontally. (Display both the cases using functions).

Logic:

- Arrays must have the same row count for concatenation.


- Columns should match in length for direct concatenation.
- For unequal rows, calculate the maximum row count.
- Pad the smaller array with spaces to match.
- Concatenate arrays horizontally using [A, B] after padding.

Code:

DYSBB22U0BID013 44
PART 1: MATLAB Date of Performance: 05-08-2025

Output:

DYSBB22U0BID013 45

You might also like