swapping columns on imported data
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
hello, i have a coursework question which needs me to organise my data imported from excel into ascending rows, where each row is also ascending. Its for pythagorian triples but for some reason itll give me the rows ascending but each row will have the largest number in the middle column not the last one. For example its giving me 3 5 4 instead of 3 4 5 like i want.
Réponses (1)
Sivsankar
le 22 Jan 2025
To address your issue, you'll need to sort each row of your matrix such that the numbers are in ascending order. Once each row is sorted, you can then sort the entire matrix by rows to ensure the rows themselves are in ascending order. Here is the workflow:
- Sort each row
- Sort the entire matrix by rows
Below is a snippet demonstrating how you can perform these steps:
% Assuming 'data' is your matrix imported from Excel
data = [3, 5, 4; 8, 10, 6; 15, 9, 12]; % Example data
% Sort each row individually
sortedRows = sort(data, 2);
% Sort the entire matrix by rows to ensure rows are in ascending order
sortedMatrix = sortrows(sortedRows);
% Display the sorted matrix
disp(sortedMatrix);
I hope this helps!
0 commentaires
Voir également
Catégories
En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!