swapping columns on imported data

5 visualizaciones (últimos 30 días)
Aimee
Aimee el 10 de Dic. de 2024
Respondida: Sivsankar el 22 de En. de 2025

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.

Respuestas (1)

Sivsankar
Sivsankar el 22 de En. de 2025
Hi @Aimee,
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:
  1. Sort each row
  2. 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);
3 4 5 6 8 10 9 12 15
I hope this helps!

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by