reshaping a matrix: adding rows and changing the selected elements of the original matrix

1 回表示 (過去 30 日間)
Take the following matrix A:
A = [ 1 2 3 4 5 6]';
If I want to find the max. and min. for a predefined frequency (e.g. frequency of 3 elements) I reshape matrix A by defininga columns for every frequency I want to evaluate. After, I can look for the min. and max. of each column.
E.g.
Frequency = 3
A1 = reshape [A,3,2];
MX1 = max(A1,[],1);
MN1 = min(A1,[],1);
So frar so good. However, does anyone have an idea of how to change this code when the frequency should increase by one step for every column? In other words, matrix A1 should actually equal the following format:
A1 =
1 2 3 4
2 3 4 5
3 4 5 6
Thank you for any advice!
best
  1 件のコメント
Jan Morawietz
Jan Morawietz 2014 年 11 月 27 日
So my plan now is to change A in advance. somth like
A11 = zeros(3,4);
for n = 1 : 2
A11(:,n) = A1(:,n)+n;
end
SO how do I make to the loop stop at 6?

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

採用された回答

Geoff Hayes
Geoff Hayes 2014 年 11 月 27 日
Jan - you could try using arrayfun to apply a function to each value in the first row, building a column for each. Perhaps something like
A1 = cell2mat(arrayfun(@(x)(x:x+2)',1:4,'UniformOutput',false));
In the above, we provide the first row of your output matrix as 1:4 or [1 2 3 4], then for each of these elements, we build a column as (x:x+2)'. As the output from arrayfun is a cell array, we need to convert it to a matrix with cell2mat. Try it and see what happens!
  2 件のコメント
Jan Morawietz
Jan Morawietz 2014 年 11 月 27 日
Thank you - it works 100% for the example above and my actual application
Geoff Hayes
Geoff Hayes 2014 年 11 月 27 日
Glad it worked out, Jan!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by