Grouping data set in each row and showing every value seperated by a "," and outputing it in a table

1 次查看(过去 30 天)
Hello,
As seen in the picture, I would like every number in the row that is not 'nan' to be grouped together and seperated by a "," and put into the 18x2 table, in its repspective row. For example, in the 18x11 figure, row 1 should be grouped as "20393,20394,20395,20396,20397,20398,20399,20400" and this result would be displayed in row 1 of the 18x2 table. As a Bonus, if you could create a thrid column in the current 18x2 table to keep a count of the total number of values reported.
Here is my current code, but as seen by the outcome in the picture, it only reports the rows that are filled with values, and compely ignores any row with 'na', as well as only reporting the first and last number in the 18x2 table
%10 sec
TenSecResultSummary = string(TenSecResult(:,1)) + '-' + string(TenSecResult(:,end));
%number of violations in each pulse
boo2=~ismember(TenSecResult,[ShortPowerViolationX]);
TenSecResult(boo2)= nan
TenSecViolationReport = string(TenSecResult(:,1))+ ',' + string(TenSecResult(:,end));
TenSecReport=table(TenSecResultSummary,TenSecViolationReport)

回答(1 个)

Satwik
Satwik 2023-6-19
Hi Youssif,
I understand that you need to manipulate the input to create a table with 3 columns filled with required data, but the current output is not matching with the required output.
I went through your code and found out that the logic you are using right now will not get you the desired results because you are not going through each and every element in the input matrix and you are just printing the first and the last numbers of the row which will not fetch you all the numbers in the row.
I have re-written the code which handles all the edge cases too including:
  1. If NaN are scattered in a row and not in adjacent columns only
  2. If there are all NaN in a single row
  3. Added the count of numbers as the third column in the table
Please look into the below code for reference
TenSecResult = Book1;
% Here I have made Book1 from the Image you have attached with the query
% TenSecResult here is a double matrix
% Calculating the size of the given Matrix
[rows cols] = size(TenSecResult);
% Looping through all the values of the matrix
for i = 1:rows
% count -> Will store number of elements in a row that are not "NaN"
count = 0;
% firstIdx -> Will store the first number in the row that is not "NaN"
firstIdx = 0;
% lastIdx -> Will store the last number in the row that is not "NaN"
lastIdx = 0;
% Will store if you have already found a number in the row that is not
% equal to "NaN"
isFirstNum = 0;
for j = 1:cols
% If the number is not "NaN"
if ~isnan(TenSecResult(i,j))
% If it is the first number then initialize both firstIdx and
% lastIdx to j
if firstIdx == 0
firstIdx = j;
lastIdx = j;
% If it is not the first number just initialise the lastidx to
% j
else
lastIdx = j;
end
% Increasing the count
count = count + 1;
% If it is the first number in the row then just add it to the
% TenSecViolationReport and make isFirstNum = 1
if isFirstNum == 0
TenSecViolationReport(i) = string(TenSecResult(i,j));
isFirstNum = 1;
% If it is not the first number then add a "," and add the
% number to the TenSecViolationReport
else
TenSecViolationReport(i) = string(TenSecViolationReport(i)) + ',' + string(TenSecResult(i,j));
end
end
% If there has been no numbers in the row
if firstIdx == 0
TenSecResultSummary(i) = "All NaN";
% Add the firstIdx and the lastIdx with a "-"
else
TenSecResultSummary(i) = string(TenSecResult(i,firstIdx)) + '-' + string(TenSecResult(i,lastIdx));
end
% Add the value of count in TenSecCount
TenSecCount(i) = count;
end
end
% In the end just convert all the matrices into column matrix
TenSecResultSummary = TenSecResultSummary.';
TenSecViolationReport = TenSecViolationReport.';
TenSecCount = TenSecCount.';
% Make it into a table
TenSecReport=table(TenSecResultSummary,TenSecViolationReport,TenSecCount);
I have also attached the output of the above code as file.
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by