swapping columns on imported data

5 ビュー (過去 30 日間)
Aimee
Aimee 2024 年 12 月 10 日
回答済み: Sivsankar 2025 年 1 月 22 日

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.

  1 件のコメント
Walter Roberson
Walter Roberson 2024 年 12 月 10 日
Hint: sortrows()

サインインしてコメントする。

回答 (1 件)

Sivsankar
Sivsankar 2025 年 1 月 22 日
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!

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by