swapping columns on imported data

5 Ansichten (letzte 30 Tage)
Aimee
Aimee am 10 Dez. 2024
Beantwortet: Sivsankar am 22 Jan. 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.

Antworten (1)

Sivsankar
Sivsankar am 22 Jan. 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!

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by