MATLAB 2D arrary problem Often you will find yourself having to sort data in 2D
ID: 2079926 • Letter: M
Question
MATLAB 2D arrary problem
Often you will find yourself having to sort data in 2D arrays. In this problem you will be sorting uniformly distributed data in a 20 by 5, sorting that data, and then calculating a statistic on the sorted data. Create a 20 by 5 array called RandomArray which contains random integers between (inclusively) -100 and 100 using randi. Sort this array in ascending order from [0, 0] to [20, 5]. Ascending meaning numbers get larger as you progress to the right and down the array. Calculate the mean of each of the 20 rows. Find the row with the average closest to zero and save that row number to a 1D array called RowNumber. Repeat steps 1-3 1000 times to get enough data points. Plot a labeled histogram of RowNumber and describe its appearance. Are the row numbers normally or uniformly distributed?Explanation / Answer
Data is uniformly distributed. Run the following code in the MATLAB
clc
clear all
close all
Length=1000;
for k=1:Length
%% Part 1
Array= randi([-100 100],[20 5]);
%% Part 2 and 3
sorted_Array=sort(Array,'ascend'); % sorts array elements in column ascend order
for i=1:20
sorted_Array1(i,:)=sort(sorted_Array(i,:),'ascend'); % sorts array in row ascend order
Rownumber1(i,k)=mean(sorted_Array1(i,:)); %% Calculates the mean of each row
end
end
Rownumber=[];
for m=1:Length
Rownumber=[Rownumber;Rownumber1(:,m)];
end
hist(Rownumber)
title('Histogram for data points')
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.