Obtaining the Percentage Error from a set of data

11 visualizzazioni (ultimi 30 giorni)
Hi everyone,
I am trying to find a way to create a vector or something similar from a set of data, as shown in the figure below, that would be read by the code below. I am trying to calculate the percentage error for each point from different runs (profile,profile1,profile2, etc.). In other words, for a single point (represented by the first column), it has seven different values so I want to see the error from one run to another. I have these files as a numeric matrix so I want them combined in one matrix of 7*200. I want to calculate the percentage error of the second column (PMF here). I have data from -4 to 4 for each run. I am unsure how to do that in a way that makes the code run well and estimate the error. I would genuinely appreciate your help. Also, I would be thankful if there is any correction for the code below in order to achieve my goal.
Thank you,
for i = 2 : num_runs
PMF_diff(i-1,:) = abs( PMF(i,:) - PMF(i-1,:) );
PMF_var(i-1,:) = PMF_diff(i-1,:) ./ abs(PMF(i-1,:));
%Different Scheme for Caluclating the Error
% PMF_diff(i-1,:) = ( abs( PMF(i,:) - PMF(i-1,:)) ).^2;
% PMF_var(i-1,:) = ( PMF_diff(i-1,:) ./ abs(PMF(i-1,:)) ).^2;
PMF_var_max(i-1) = max(PMF_var(i-1,53:148));
end

Risposte (1)

vidyesh
vidyesh il 4 Apr 2024
Hello Hasan,
Based on the requirements for combining seven matrices, there are two scenarios to consider depending on the uniformity of the data in the first column of each matrix. Here's how to approach both:
  • When the first column of each matrix is the same
If the first column of each matrix is identical, indicating synchronized indices, you can directly combine the second columns of all matrices. Consider the following example matrices A1 to A7:
A1 =[-4:0.1:4 randi(100,1,81)];
A2 =[-4:0.1:4 randi(100,1,81)];
A3 =[-4:0.1:4 randi(100,1,81)];
A4 =[-4:0.1:4 randi(100,1,81)];
A5 =[-4:0.1:4 randi(100,1,81)];
A6 =[-4:0.1:4 randi(100,1,81)];
A7 =[-4:0.1:4 randi(100,1,81)];
col2_A1 = A1(:,2);
col2_A2 = A2(:,2);
col2_A3 = A3(:,2);
col2_A4 = A4(:,2);
col2_A5 = A5(:,2);
col2_A6 = A6(:,2);
col2_A7 = A7(:,2);
% Combine these columns to form a new array of size N x 7
result = [col2_A1 col2_A2 col2_A3 col2_A4 col2_A5 col2_A6 col2_A7];
% result is the final array of size N x 7
  • When only common indices should be merged
In cases where the first column (acting as an index) differs among matrices and matrices need to merge based on common indices, use the following function:
function result = mergeColumnsOnIndices(arrays)
% arrays is a cell array containing your input arrays, e.g., {A1, A2, A3, ..., A7}
% Initialize with the indices of the first array
commonIndices = arrays{1}(:, 1);
% Loop to find common indices across all arrays
for i = 2:length(arrays)
commonIndices = intersect(commonIndices, arrays{i}(:, 1));
end
% Initialize the result matrix with the correct number of rows and 7 columns
result = zeros(length(commonIndices), length(arrays));
% For each array, find the rows with common indices and extract the second column
for i = 1:length(arrays)
[~, idx] = intersect(arrays{i}(:, 1), commonIndices);
result(:, i) = arrays{i}(idx, 2);
end
end
Hope this helps.

Community Treasure Hunt

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

Start Hunting!

Translated by