Wednesday, 3 August 2016

MATLAB Programming 45 - Array functions for dimentions


Length, Dimension and Number of elements:


Create a script file and type the following code into it:


x = [7.1, 3.4, 7.2, 28/4, 3.6, 17, 9.4, 8.9];
length(x) % length of x vector
y = rand(3, 4, 5, 2);
ndims(y) % no of dimensions in array y
s = ['Zara', 'Nuha', 'Shamim', 'Riz', 'Shadab'];
numel(s) % no of elements in s

When you run the file, it displays the following result:


ans =
8

ans =
4

ans =
23

Circular Shifting of the Array Elements:


Create a script file and type the following code into it:


a = [1 2 3; 4 5 6; 7 8 9] % the original array a
b = circshift(a,1) % circular shift first dimension values down by 1.
c = circshift(a,[1 -1]) % circular shift first dimension values % down by 1
% and second dimension values to the left % by 1.

When you run the file, it displays the following result:


a =
1 2 3
4 5 6
7 8 9

b =
7 8 9
1 2 3
4 5 6

c =
8 9 7
2 3 1
5 6 4


No comments:

Post a Comment