r = 5;
A_r5 = (S_Vals(1,1)*u(:,1)*(V(:,1)).')+(S_Vals(2,1)*u(:,2)*(V(:,2)).')+(S_Vals(3,1)*u(:,3)*(V(:,3)).')+(S_Vals(4,1)*u(:,4)*(V(:,4)).')+(S_Vals(5,1)*u(:,5)*(V(:,5)).');
x_5 = uint8(A_r5);
figure(2)
imshow(x_5);

Antworten (2)

Image Analyst
Image Analyst am 16 Feb. 2022

0 Stimmen

Yes, that's very complicated to look at. I'd break it into more bite-sized terms, like
term1 = whatever
term2 = whatever
A_r5 = term1 * term2 + term3 or whatever.

11 Kommentare

I want to sum the following in a for loop
term 1 = A(1,1)*U(:,1),V(:,1)
term 2 = A(2,1)*U(:,2),V(:,2)
term 1 = A(3,1)*U(:,3),V(:,3)
A_r = term1 + term2 + term3
thanks
Image Analyst
Image Analyst am 16 Feb. 2022
I don't understand the expressions. Please check the math.
A(1,1)*U(:,1) is one term, but V(:, 1) is another term and you just put them on the same line with a comman between them. What's that supposed to do?
Im sorry the correct expression is
term 1 = A(1,1)*U(:,1)*V(:,1)
term 2 = A(2,1)*U(:,2)*V(:,2)
term 1 = A(3,1)*U(:,3)*V(:,3)
A_r = term1 + term2 + term3
for any given number of terms
Do you mean like
[rows, columns] = size(A);
A_r = zeros(rows, 1);
% Rows must equal the number of columns in both U and V or this won't work.
for row = 1 : rows
term1(row) = A(row,1)*U(:, row)*V(:, row)
end
A_r = sum(term1);
Try
A = sum(S .* U .* V);
Im trying create a simple for loop to compute the summation for
r = 100
i = 1:r
A100 = S(i,1)*U(:,i)*V(:,i);
Image Analyst
Image Analyst am 16 Feb. 2022
Are those matrix products or element by element products?
S,U, and V are all matrices
Image Analyst
Image Analyst am 16 Feb. 2022
I still don't know what you want so I'll give others a chance. You're multiplying a row or column of a matrix (which would be a 1-D vector) times a matrix and I don't know if you want every row multiplied by that row or what. I'm totally confused.
THANKS anyway
DGM
DGM am 16 Feb. 2022
Need to transpose V(:,row).'

Melden Sie sich an, um zu kommentieren.

DGM
DGM am 16 Feb. 2022
Bearbeitet: DGM am 16 Feb. 2022

0 Stimmen

Depends what you mean by "simple". Consider the following three options. Method 2 is compact, but it's slower for large inputs. Method 3 is relatively fast for large inputs. Both method 2 and 3 have an advantage in that they can be scaled to more than 5 "pages".
N = 1000; % large inputs
u = rand(N,5);
V = rand(N,5);
S_Vals = rand(5,1);
% original method
a = timeit(@() testA(S_Vals,u,V))
a = 0.0031
% simple permutation and elementwise math
b = timeit(@() testB(S_Vals,u,V))
b = 0.0132
% looped version of original
c = timeit(@() testC(S_Vals,u,V))
c = 0.0012
% time ratio
[a/b a/c]
ans = 1×2
0.2365 2.6443
function testA(S_Vals,u,V)
A_r5 = (S_Vals(1,1)*u(:,1)*(V(:,1)).') ...
+(S_Vals(2,1)*u(:,2)*(V(:,2)).') ...
+(S_Vals(3,1)*u(:,3)*(V(:,3)).') ...
+(S_Vals(4,1)*u(:,4)*(V(:,4)).') ...
+(S_Vals(5,1)*u(:,5)*(V(:,5)).');
end
function testB(S_Vals,u,V)
A_r5 = sum(permute(S_Vals,[3 2 1]).*permute(u,[1 3 2]).*permute(V,[3 1 2]),3);
end
function testC(S_Vals,u,V)
A_r5 = zeros(size(u,1),size(V,1));
for k = 1:size(S_Vals(1))
A_r5 = A_r5 + (S_Vals(k,1)*u(:,k)*(V(:,k)).');
end
end

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Tags

Kommentiert:

DGM
am 16 Feb. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by