Finding the Power density function

I am trying to find the power density function of the 8 datasets, attached below. Can anyone help me?

3 Comments

"When I call PDF(2) I just see the 2nd integer numer from the last 1000x1. I would think I would see the 2nd 1000x1"
Your code does not use any indexing when allocating to the variable PDF, instead on each loop iteration your code simply replaces PDF completely, so in the end you will have only the vector(?) of data from the last loop iteration. That is the vector that you are indexing into.
Note how your other loops use indexing to allocate to the preallocated output array, e.g.:
data3(x)= ...
% ^^^ indexing to allocate into data3
whereas there is no indexing for allocating the results of the last loop:
PDF = test/length(i);
% ^^ where is the indexing?
Note that the loop iterator i will be a scalar and length of a scalar is one, so your code always divides by one.
Note that numbering variables like that is a sign that you are doing something wrong.
Note that copy-and-pasting code like that is a sign that you are doing something wrong.
Both of these conspire to make this task more verbose and more complex. Rather than writing separate code for importing the data from each file you should use a simple loop, just like the MATLAB documentation recommends:
Note how the examples use a loop and store all of the imported data in one cell array. You can then use indexing to access the file data (i.e. given the mydata cell array, the 2nd file data would be simply mydata{2}, exactly as you describe).
If the content of that cell array have suitable sizes then they could be concatenated into one matrix, e.g.:
mat = [mydata{:}]
Basic approach could be FFT.
Also if you have any question on your code, you should use code format from the tooltip like this:
a = 1
b = 2
x = 1:10
y = sin(a*x)
Also you may want to have a good read of the guideline to get answers quickly.
Thank you. I got the PDF, but still having issues with the for loop. I tried indexing it, but now I get all 0's in colums 2-7....
%% import packages %%
%pkg load statistics
%pkg load signal
%% import values %%
fid=fopen('Data1.txt','r');
data1=fscanf(fid,'%f');
fclose(fid);
fid=fopen('Data2.txt','r');
data2=fscanf(fid,'%f');
fclose(fid);
fid=fopen('Data3.txt','r');
data3prime=fscanf(fid,'%f');
fclose(fid);
data3=zeros(10000,1);
for i= (1:20000);
if mod(i,2)==0;
x=i*0.5;
data3(x)=data3prime(i);
end
end
fid=fopen('Data4.txt','r');
data4=fscanf(fid,'%f');
fclose(fid);
fid=fopen('Data5.txt','r');
data5prime=fscanf(fid,'%f');
fclose(fid);
data5=zeros(10000,1);
for i= (1:20000);
if mod(i,2)==0;
x=i*0.5;
data5(x)=data5prime(i);
end
end
fid=fopen('Data6.txt','r');
data6=fscanf(fid,'%f');
fclose(fid);
fid=fopen('Data7.txt','r');
data7=fscanf(fid,'%f');
fclose(fid);
fid=fopen('Data8.txt','r');
data8=fscanf(fid,'%f');
fclose(fid);
%dataset = [data1,data2, data3, data4, data5, data6, data7,data8]
%% Plot Signal %%
x = (1:10000);
%plot(x,data8(1:10000));
%title('Data8');
%hold on;
%plot(x,data2(1:1000));
%plot(x,data3(1:1000));
%plot(x,data4(1:1000));
%plot(x,data5(1:1000));
%plot(x,data6(1:1000));
%plot(x,data7(1:1000));
%plot(x,data8(1:1000));
%legend('data1','data2','data3','data4','data5','data6','data7','data8');
%hold off;
%PDF = []
%% Probality Density Functions
% finding the PDF for each column
PDF = fft(dataset)
%% Statistical Moment (Mean,StandardDeviation,Skewness and Kurtosis(AKA Flatness Factor))
% col = dataset(:,1:8);
% p1(i) = dataset(i)(:,1)
%% Standard Deviation %%
for i = size(dataset,2)
dataset = [data1,data2, data3, data4, data5, data6, data7,data8];
col(:,i) = dataset(:,i)
end

Sign in to comment.

Answers (1)

Hi Christina Reid,
I understand that you are not getting expected output while printing columns of dataset. This is happening because of the following reasons:
  • This loop, due to its condition i = size(dataset,2), does not actually loop in the traditional sense. It sets i" to the number of columns in dataset and then runs the body of the loop once with i being that value. If dataset initially has 8 columns, i" is set to 8.
  • “col(:,i) = dataset(:,i) attempts to copy the i-th column of dataset into col. However, since i is only the last column's index due to the loop's setup, this operation only copies the last column.
To enhance the code and avoid the unintended redefinition of "dataset" inside the loop, you should move the construction of the "dataset" matrix outside and before the loop. This ensures that "dataset" is defined only once, and the loop is then used solely for processing each column of this dataset.
Modified Code of “for” loop will look like:
dataset = [data1,data2, data3, data4, data5, data6, data7,data8];
for i = 1:size(dataset,2)
col(:,i) = dataset(:,i)
end

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 3 Feb 2021

Answered:

on 21 May 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!