running a code on all columns

1 回表示 (過去 30 日間)
DARLINGTON ETAJE
DARLINGTON ETAJE 2019 年 8 月 14 日
編集済み: KSSV 2019 年 8 月 14 日
Consider, Y is a 1000 by 100 matrix...100 columns and 1000 rows
Y1=Y(:,1);
if I run this code
S1 = numel(Y1);
xx1 = reshape(Y1(1:S1 - mod(S1, 20)), 20, []);
if I run this code
xx1 is a 20 by 50 matrix
What code can I use to do the same thing for the remaining 99 columns of Y

採用された回答

KSSV
KSSV 2019 年 8 月 14 日
編集済み: KSSV 2019 年 8 月 14 日
Y = rand(1000,100) ;
iwant = zeros(20,50,100) ;
for i = 1:100
Y1 = Y(:,i) ;
S1 = numel(Y1); % this is same always 1000
iwant(:,:,i) = reshape(Y1(1:S1 - mod(S1, 20)), 20, []);
end
With out loop
% no loop
m = 1000 ;
n = mod(m,20) ;
P = Y(1:m-n,:) ;
Q = reshape(P,20,50,[]) ;
  3 件のコメント
DARLINGTON ETAJE
DARLINGTON ETAJE 2019 年 8 月 14 日
Asking the question further:
Consider, Y is a 1000 by 100 matrix...100 columns and 1000 rows
Y1=Y(:,1);
if I run this code
S1 = numel(Y1);
xx1 = reshape(Y1(1:S1 - mod(S1, 20)), 20, []);
y1 = sum(xx1, 1).' / 20;
y1 is a 50 by 1 matrix
assuming z is also a 50 by 1 matrix
subtr=abs(y1-z)
What code can I use to do the same thing for the remaining 99 columns of Y
KSSV
KSSV 2019 年 8 月 14 日
編集済み: KSSV 2019 年 8 月 14 日
If you want same to repeat for every column ..you can run a loop...but there are options to avoid looping.

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by