作者celestialgod (天)
看板MATLAB
標題Re: [討論] 取出 Jordan blocks
時間Mon Aug 24 16:16:55 2015
※ 引述《math99 (新世界)》之銘言:
: 我有一個 Jordan form , 我想要取出各別的 Jordan blocks
: 請教 matlab 有什麼方便的做法嗎?
: 目前我做法只能用 for loop 一個一個取, 希望能夠不要用到 loop
: 例
: Input:
: A = [2 0 0 0;
: 0 1 1 0;
: 0 0 1 0;
: 0 0 0 2]
: Output:
: J1 = [2], J2=[1,1;0,1], J3=[2];
: 感謝
indices = mat2cell(1:size(A, 1), 1, [1, 2, 1]);
out = cellfun(@(x) A(x,x), indices, 'UniformOutput', false);
out會有三個element, [2], [1,1;0,1], [2]
至於[1, 2, 1],目前沒有想到更好的方法去抓出來
後來想到用下面的方法來做,目前測試還未有BUG出現
有其他問題請麻煩通知我
% matrix generation
bksize = randi(3, 4, 1);
As = arrayfun(@(x) rand(x), bksize, 'UniformOutput', false);
A = blkdiag(As{:});
% extract blocks
[I, J] = ind2sub(size(A), find(A ~= 0));
I = sort(I);
repeatLength = diff([0; find(I(2:end) ~= I(1:end-1)); length(I)]);
repeatLoc = [find(repeatLength(2:end) ~= repeatLength(1:end-1)); ...
length(repeatLength)];
bksize = repeatLength(repeatLoc);
if sum(bksize) ~= size(A,1)
tmp = diff([0; repeatLoc]) ./ bksize;
bksize = cell2mat(arrayfun(@(x, y) x * ones(y, 1), bksize, tmp, ...
'UniformOutput', false));
end
indeices = mat2cell(1:size(A, 1), 1, bksize);
out = cellfun(@(x) A(x,x), indeices, 'UniformOutput', false);
isequal(As, out') % true
我稍微改寫了一下 放到matlab的file exchange去了~~
網址:
http://tinyurl.com/oev2p98
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.205.27.107
※ 文章網址: https://www.ptt.cc/bbs/MATLAB/M.1440404218.A.9A1.html
推 sunev: jordan form只要判斷diag(A,1)即可 08/24 18:43
我已經忘記jordan form是啥了XDDD 四年前學的東西 沒用到過~"~
寫成general form就都可以用了~~~
推 sunev: A=[1 0 1;0 1 0;0 0 1]; ?08/25 12:47
這個我沒辦法QQ
→ math99: 感謝 好漂亮用法08/25 17:58
→ math99: s大舉得例我應該不會遇到08/25 17:59
推 math99: 在請教一下 cellfun 參數 'UniformOutput' 作用是什麼?08/25 18:03
如果每一個輸出不是長度為一的向量,就要把這個選項設定false
※ 編輯: celestialgod (123.205.27.107), 08/25/2015 18:13:54
推 math99: ok, thanks 08/25 18:16