Filter löschen
Filter löschen

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

3 Ansichten (letzte 30 Tage)
Khan
Khan am 23 Jun. 2021
Kommentiert: Khan am 23 Jun. 2021
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

Akzeptierte Antwort

KSSV
KSSV am 23 Jun. 2021
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

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 23 Jun. 2021
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

Kategorien

Mehr zu Multidimensional Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by