- First, input the matrix from the user and determine its dimensions.
- For each row, compute the absolute values of its elements and sort these values in descending order.
- During this sorting process, keep track of the original indices to maintain a reference to the initial positions of the elements.
- Construct a formatted string for each row, where each element is displayed with its original sign and followed by its index in parentheses.
- This involves iterating over the sorted indices and using them to access the original values in the matrix.
- The formatted strings for each row are stored in a cell array, and finally, the results are displayed in a user-friendly format, showing the sorted elements with their corresponding indices.
Sort absolute value of matrix row in descending order, display with +,- sign along with original index value
5 次查看(过去 30 天)
显示 更早的评论
I have a nXm matrix with its values from user and i want to sort only absolute values of its rows in descending order and display the original values of rows in +,- sign along with its original index value in bracket in the form of a table.
e.g
r1=[-5,-4,2,6]
ans: [6(4), -5(1), -4(2) ,2(3)]
This is the code I have written:
% generating the table of rows and columns
row = input('Enter number of rows: ');
col = input('Enter number of columns: ');
C = zeros(row ,col);
%taking values from user of matrix
for s = 1:row
for l = 1:col
str = ['Enter element in row ' num2str(s) ', col ' num2str(l) ': '];
C(s,l) = input(str);
end
end
%taking absolute values of all elements
D=abs(C);
T=array2table(D);
%sorting the rows in descending order
B = sort(T,'descend');
But I am having issues with generating index and displaying it along with brackets as mentioned in above example.
Please help me.
Thank you!!
0 个评论
回答(1 个)
Arjun
2024-9-12
Hi,
I see that you have a matrix, and you want to sort the rows of the matrix based on the absolute value of the contents in descending order and then display the result with sign and corresponding original positions in a specific format.
To sort the rows of a matrix based on the absolute values of their elements while preserving the original values and indices we can follow the following steps:
Please refer to the attached code for better understanding of the flow:
% Example matrix input
matrix = [-5, -4, 2, 6; 3, -7, 1, -2; 8, -3, 0, 4];
% Number of rows and columns
[n, m] = size(matrix);
% Initialize a cell array to store results
resultTable = cell(n, 1);
% Process each row
for i = 1:n
% Extract the current row
currentRow = matrix(i, :);
% Get absolute values and sort them in descending order
[~, sortedIndices] = sort(abs(currentRow), 'descend');
% Create a string to hold the sorted row with original indices
sortedRowStr = '';
% Build the sorted row with original values and indices
for j = 1:m
originalIndex = sortedIndices(j);
value = currentRow(originalIndex);
sortedRowStr = [sortedRowStr, sprintf('%+d(%d) ', value, originalIndex)];
end
% Trim the trailing space and store the result
resultTable{i} = strtrim(sortedRowStr);
end
% Display the results
disp('Sorted rows with original indices:');
for i = 1:n
fprintf('Row %d: %s\n', i, resultTable{i});
end
I hope this will help!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!