0

My particular application requires me to do the following:

  • Take SVD decomposition of a slab of a tensor (lets call this tensor F of size [Nr,Nt,Nsub])
  • Get the right singular matrix of this slab (lets call it matrix V)
  • multiply the slab with subset of the right singular matrix
  • Multiply the result by another vector (s of dimension nx1)
for k=1:size(F,3)
    H= squeeze(F(:,:,k));
    [U,Sigma,V]= svd(H);
    V= V(:,1:n);
    Y(:,k) = H * V* s; 
end 

My question is: does this matrix multiplication make any sense, or am I doing something wrong in the sense that I have implicity a three dimension matrix being multiplied by a 2 D matrices... in other words am I overwriting values?

If my question isnt clear I can edit it.. Thank you.

Tyrone
  • 167
  • 1
  • 10
  • 1
    You’re not overwriting values, but you should preallocate `Y` before the loop. Whether this makes sense depends on the meaning of the data. – Cris Luengo Aug 18 '18 at 15:24
  • Thank you for replying, what does preallocating mean? can you please give me an example? Do you mean initialize Y = zeros(Nr,Nlayer)? Thank you @CrisLuengo – Tyrone Aug 18 '18 at 18:17
  • Yes, that is exactly it. I didn’t see that in your bit of code. Preallocation is not strictly necessary, but speeds up the code. In this case, though, it might be required? – Cris Luengo Aug 18 '18 at 18:20
  • Thank you Cris. I can definitely check. One more question, i hope you can help me further. You see I wwrote this question not too long ago and its quite similar idea: https://stackoverflow.com/questions/51548250/how-come-these-two-matrices-are-not-equivalent?noredirect=1#comment90066104_51548250 – Tyrone Aug 18 '18 at 18:26
  • I received an answer that what I was doing in the previous question is wrong, I am wondering how come what I am doing here is OK, but in my previous question I was overwriting... @CrisLuengo.. thank you/ – Tyrone Aug 18 '18 at 18:27
  • In that question, the first bit of code does overwrite because you loop over 3 variables, but only used 2 to index into `Y`. The other bit of code, after the edit, doesn’t overwrite data. But that doesn’t mean you can reverse the operation, that is a different question. – Cris Luengo Aug 18 '18 at 18:37
  • Thank you. So basically the bit of code in this question is similar to the code in my first question AFTER the edit.. its not wrong but it can't be reversed? – Tyrone Aug 18 '18 at 18:43
  • and I couldnt understand why I wasnt able to reverse the operation but the writing the other bit of code Y(:,k)= squeeze(F(:,:,k)) *X(:,k)...sorry so many questions. but this has been bothering me for while. thank you – Tyrone Aug 18 '18 at 18:50
  • A matrix multiplication can, in general, not be reversed. Same as `y=1+2+3`, you cannot derive the three numbers from the one value of `y`. – Cris Luengo Aug 18 '18 at 19:04
  • Instead of `V(:,1:n)` you could have written `V(:,1:end)` if this si the full dimension of `V`. But also by doing this you've picked the whole matrix `V`. Is `V` expected to be square? – Mefitico Aug 31 '18 at 18:22

0 Answers0