matlab中row,MATLAB中的sort、sortrow用法

本文详细介绍了MATLAB中的sort和sortrows函数的使用方法。sort函数用于一维和二维数组的升序排序,可以结合索引来实现降序排序。sortrows则用于按矩阵的指定列进行排序,保持其他列对应关系不变。同时,文章通过多个示例展示了如何进行排序操作,并提供了按行排序和按列排序的实例。
摘要由CSDN通过智能技术生成

sort(A)若A是向量不管是列还是行向量,默认都是对A进行升序排列。sort(A)是默认的升序,而sort(A,'descend')是降序排序。

sort(A)若A是矩阵,默认对A的各列进行升序排列

sort(A,dim)

dim=1时等效sort(A)

dim=2时表示对A中的各行元素升序排列

看下面的例子

>> A=magic(3)

A =

8 1 6

3 5 7

4 9 2

>> sort(A)

ans =

3 1 2

4 5 6

8 9 7

>> sort(A,1)

ans =

3 1 2

4 5 6

8 9 7

>> sort(A,2)

ans =

1 6 8

3 5 7

2 4 9

===================

Matlab中给一维向量排序是使用sort函数:sort(A),排序是按升序进行的,其中A为待排序的向量;若欲保留排列前的索引,则可用 [sA,index]

= sort(A) ,排序后,sA是排序好的向量,index 是

向量sA 中对 A 的索引。 索引使排列逆运算成为可能。

事实上,这里A 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是MATLAB实现Huffman编码的huffmandict函数的源代码: ```matlab function [dict, avglen] = huffmandict(symbols, prob) %HUFFMANDICT Create Huffman dictionary. % [DICT,AVGLEN] = HUFFMANDICT(SYMBOLS,PROB) returns a cell array DICT % containing a Huffman dictionary for the source symbols in SYMBOLS. % It also returns the average code length for the source symbols. % SYMBOLS is a row vector of symbols that can be of any data type % (for example, uint8, char, or double). PROB is a row vector of % probabilities corresponding to each symbol. The sum of the % probabilities must be 1.0. % % Each row of DICT is a two-element cell array. The first element is % the source symbol, and the second element is a binary Huffman code % for that symbol. % % Example: % symbols = ['A' 'B' 'C' 'D' 'E']; % prob = [0.1 0.2 0.3 0.2 0.2]; % [dict, avglen] = huffmandict(symbols, prob); % disp(dict); % disp(avglen); % % See also HUFFMANENCODE, HUFFMANDECODE. % Copyright 2002-2017 The MathWorks, Inc. % Validate input arguments narginchk(2, 2); if ~isvector(symbols) error(message('comm:huffmandict:SymbolsNotVector')); end if ~isvector(prob) error(message('comm:huffmandict:ProbNotVector')); end symbols = symbols(:); prob = prob(:); if ~isequal(length(symbols), length(prob)) error(message('comm:huffmandict:SymbolProbMismatch')); end if ~isreal(prob) || any(prob < 0) || any(prob > 1) || abs(sum(prob) - 1) > eps error(message('comm:huffmandict:InvalidProbabilities')); end % Sort symbols and probabilities in decreasing order of probability [prob, idx] = sort(prob, 'descend'); symbols = symbols(idx); % Number of source symbols numSymbols = length(symbols); % Initialize Huffman tree tree = zeros(2 * numSymbols - 1, 3); % Initialize leaves of tree tree(1:numSymbols, 3) = prob; tree(1:numSymbols, 1) = (1:numSymbols)'; % Initialize remainder of tree nextnode = numSymbols + 1; for i = 1:numSymbols - 1 % Find smallest two probabilities [junk, S] = min(tree(1:i, 3)); tree(i, 2:3) = tree(S(1), 1:2); tree(i, 3) = tree(S(1), 3) + tree(S(2), 3); tree(S(2), :) = []; % Add a new node tree(nextnode, 1:2) = i + numSymbols; tree(nextnode, 3) = sum(tree(i + numSymbols, 3)); nextnode = nextnode + 1; end % Generate the codebook dict = cell(numSymbols, 2); for i = 1:numSymbols code = ''; node = i + numSymbols - 1; while node ~= 1 if tree(tree(node, 4), 1) == node code = ['0' code]; else code = ['1' code]; end node = tree(node, 4); end dict{i, 1} = symbols(i); dict{i, 2} = code; end % Compute the average codeword length avglen = sum(prob .* cellfun('length', dict(:, 2))); ``` 该函数接受两个向量,一个包含符号,另一个包含每个符号的概率。它返回一个单元格数组,其每行包含一个符号和与该符号相关联的Huffman编码。它还返回源符号的平均码长度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值