MATLAB Thus far you know several kinds of containers available in MATLAB: vector
ID: 3813535 • Letter: M
Question
MATLAB
Thus far you know several kinds of containers available in MATLAB: vectors, matrices, cell arrays, and structs. In many other languages, a container has a unique set of methods available to each object of the class. You are to create a vector class that operates closely to the vector container in C++.
Create a vector value class with two private properties: Contents and Size.
Implement the following methods:
Vector Constructor - initialize Contents to empty vector and set the Size to 0
push_back(value) - takes number and pushes it into the vector at the end, increases size by 1
pop_back() - takes no input, removes the element at the end of the vector, decreases size by 1
at(index) - takes an integer as index, returns the value at that index. For example: vec.at(2) returns the 2nd element
size() - takes no input, returns the current size of the vector
empty() - takes no input, returns logical true or false if the vector is empty
insert(index, value) - replaces value at index in Contents vector.
Follow all instructions carefully to ensure you understand what each method does.
SOLUTION **THIS IS JUST AN OUTLINE THAT NEEDS TO BE FILLED IN**
classdef Vector
properties
% implement Contents and Size props
end
methods
% Constructor
function vector = Vector()
end
% push_back
% pop_back
% at
% size
% empty
% insert
end
end
Explanation / Answer
classdef Vector
properties
% implement Contents and Size props
content
size
end
methods
% Constructor
function vector = Vector()
content=[]
size=0
end
% push_back
function p=push_back(a)
size=size+1
content(size)=a
end
% pop_back
function q=pop_back(b)
p=content(size)
size=size-1
end
% at
function r=at(ind)
r=content(ind)
end
% size
function s=get_size()
s=size
end
% empty
function emp=check_empty()
if size==0
emp=true
else
emp=false
end
end
% insert
function ins=insert(ind,val)
contents(ind)=val
end
end
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.