필터 지우기
필터 지우기

how to save values for changing number of columns using "for loop "

조회 수: 3 (최근 30일)
Khan
Khan 2021년 6월 23일
댓글: Khan 2021년 6월 23일
I want to run a simple code, but i am unable to get the results correctly. A simple code is wriiten below, I am changing number of columns of one parameter. for that i want to save value of X_telda (10), X_telda(50) and X_telda(100) separately. U and X are given matrices.
for r=[10 50 100];
X_telda=(U(:,1:r)*(U(:,1:r)'*X));
end
thank you

채택된 답변

KSSV
KSSV 2021년 6월 23일
r=[10 50 100] ;
n = length(r) ;
X_telda = cell(n,1) ;
for i = 1:n
X_telda{i} = (U(:,1:r(i))*(U(:,1:r(i))'*X));
end
X_telda is a cell, you can access it using X_telda{1},..etc

추가 답변 (1개)

Walter Roberson
Walter Roberson 2021년 6월 23일
We do not know the size of U. Call it N x M
U(:,1:r) is an N x r matrix
U(:,1:r)' is an r x N matrix.
X we do not know the size of, but in order to * it by an r x N matrix, we can tell that X must be N x something. Call it N x P
So U(:,1:r)' * X would be r x N * N x P giving r x P
U(:,1:r) * that would be N x r * r * P, so the overall operation would give N x P .
To put it another way, X_telda is size(U,1) by size(X,2)
Your code does not have any obvious problems, other than the fact that you are not storing the results X_telda before overwriting them in the next loop. But how do you want the multiple outputs represented?
Well, one way would be to use:
rvals = [10 50 100];
numr = length(rvals);
X_telda = zeros(size(U,1), size(X,2), numr);
for ridx = 1 : numr
r = rvals(ridx);
X_telda(:,:,ridx) = U(:,1:r)*(U(:,1:r)'*X));
end

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by