matrix indexing and for loops
27 次查看(过去 30 天)
显示 更早的评论
Hi I am still learning the ropes with Matlab and have another problem I am not sure how to get round.
I have a matrix Z which is 12x65044 and I would like to peform an operation on each individual column of the matrix. How can I do this automatically?
Perhaps something along the lines of for: Z columns(1:65044) ... perform desired function ... and then store the output vector from each function into one new matrix say Z2???
Thanks for help
4 个评论
Sean de Wolski
2011-7-19
@scour_man: Not being familiar with ARMASA, I have no idea.
@Jan: I was wondering if the function was numerical - we could then probably do it with a convolution.
采纳的回答
Andrei Bobrov
2011-7-19
1. Loop
Z2 = zeros(size(Z));
for j1 = 1:size(Z,2)
Z2(:,j1) = fun(Z(:,i1)); % ... perform desired function 'fun'...
end
2. Use 'arrayfun'
Z2 = cell2mat(arrayfun(@(i1)fun(Z(:,i1)),1:size(Z,2),'un',0));
3. In some cases, you can use function 'bsxfun'.
ADD if the lengths of the vectors 'pred' may be different
1.
n = size(Z,2);
pred = cell(1,n);
covpred = pred;
for j1 = 1:n
[pred{j1}, covpred{j1}] = arma2pred(a,b,Z(:,j1),Lpred);
end
2.
[pred,covpred] = arrayfun(@(i1)arma2pred(a,b,Z(:,i1),Lpred),1:size(Z,2),'un',0));
10 个评论
Andrei Bobrov
2011-7-21
%so
a = cell(1,size(Z,2));
b = a;
Z2=a;
for j1=1:size(Z,2),
[a{j1} b{j1}]=armasel(Z(:,j1));
Z2{j1}=arma2pred(a{j1},b{j1},Z(:,j1),3);
end
%if the values of 'a' and 'b' are needed in the future
% if not, then
Z2= cell(1,size(Z,2));
for j1=1:size(Z,2),
[a, b]=armasel(Z(:,j1));
Z2{j1}=arma2pred(a,b,Z(:,j1),3);
end
更多回答(1 个)
Jan
2011-7-20
Usually error messages do not tell only the reason of an error, but also the location. And without knowing in which line the error appears, it is impossible to fix it. Without the location, an error message is not "complete".
A more direct solution of your problem would be to use the debugger to let Matlab stop, when the error appears:
dbstop if error
Then inspect the contents of the used variables in the command window or workspace browser. Then you will find out, that the dimension of the lefthand side and the righthand side of an assignment do not match. E.g. "Z2(:, j1)" is a vector, but arma2pred replies an array of a different size, e.g. empty, or matrix.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!