Hi all,
Imagine I have a 2-d cell, each cell element is a m by n matrix, I apply SVD on each element such that they are decomposed into left singular vectors, singular values and right singular vectors. Here's an example:
clear; clc;a = 2;b = 3;matL = 5;matW = 4;myCell = cell(a, b);myCell = cellfun(@(v) rand(matL, matW), myCell, 'UniformOutput', false);[myCellL, myCellSig, myCellR] = ... cellfun(@(v) svd(v, 'econ'), myCell, 'UniformOutput', false);
Now I'd like to extract the first vector of myCellL, first value of myCellSig, first vector of myCellR, put these into one cell element, and repeat for the second, third…… I can write a loop to do this (continue the above script):
myOtpt = cell(a, b, matW);for i = 1:a for j = 1:b for k = 1:matW myA = myCellL{i, j}(:, k); myB = myCellSig{i, j}(k, k); myC = myCellR{i, j}(:, k); myOtpt{i, j, k} = {myA, myB, myC}; end endend
The second part of script (for loops) seems also is a repeat operation on cells 'myCellL, myCellSig, myCellR', is it possible to write these with cellfun, such that for loop is avoided?
Many thanks!
Best Answer