matrix indexing and for loops

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 comentarios

Sean de Wolski
Sean de Wolski el 19 de Jul. de 2011
What is the function? It can probably (hopefully) be done without loops or arrayfun, but we need to know the operation.
scour_man
scour_man el 19 de Jul. de 2011
thanks for help. the function is arma2pred from the ARMASA toolbox. The function is like so:
[pred covpred]=arma2pred(a,b,data,Lpred) where a and b are the AR and MA parameters vectors, Lpred is the length of predictions and data in this case I need to be each column of the matrix Z. I would like the vector of predictions (pred) from each column returned to the matrix Z2 if this is possible?
Jan
Jan el 19 de Jul. de 2011
Yes, it is possible and already posted by Andrei.
Sean de Wolski
Sean de Wolski el 19 de Jul. de 2011
@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.

Iniciar sesión para comentar.

 Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 19 de Jul. de 2011

2 votos

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 comentarios

Jan
Jan el 19 de Jul. de 2011
ARRAYFUN is hip, but I do not like CELL2MAT for reasons of efficiency. My favourite is the simple FOR loop: It is fast in modern Matlab versions, but even more important: The debug time is optimal in case of problems or modifications. +1
Andrei Bobrov
Andrei Bobrov el 20 de Jul. de 2011
Hi Jan, I agree with you and remember our "research" question the benefits loops over other methods
scour_man
scour_man el 20 de Jul. de 2011
thanks for your help but something I am doing is not quite right as I get ??? subscripted assignment dimension mismatch. My code is:
Z2=zeros(size(Z));
for j1=1:size(Z,2)
Z2(:,j1)=arma2pred(a,b,Z(:,j1),Lpred)
end
It may be because the output from the funtion is two vectors and I only want the first, how do I do this? Thanks
Jan
Jan el 20 de Jul. de 2011
@scour_man: The shown code seems to be correct. If just one output is used, the first is taken and the 2nd is ignored, as wanted.
Please post the complete error message. I assume the error appears *inside* the function arma2pred.
scour_man
scour_man el 20 de Jul. de 2011
@Jan Simon: The error message is simply: ??? subscripted assignment dimension mismatch
Andrei Bobrov
Andrei Bobrov el 20 de Jul. de 2011
try variant
Z2 = cell(1,size(Z,2))
for j1=1:size(Z,2),
Z2{j1}=arma2pred(a,b,Z(:,j1),Lpred);
end
cellfun('size',Z2,1)
% so you will know the length of vectors in the cells of Z2
scour_man
scour_man el 20 de Jul. de 2011
@ andrei: Thanks that works using cell and after I use cell2mat to extract and then reshape the data as necessary. The next problem is that I wish to use a different a and b each time. a and b are calculated using the function: [a b]=armasel(data)
In this case I want the data to be each column of the matrix Z. So for example the code will calculate the variables a and b for column 1 then use these outputs in the arma2pred function on column 1 of Z and save output to column 1 of Z2. Then a new a and b are calculated using the armasel function on column 2 of Z and these values are used in the arma2pred calculation on column 2 of Z and the outputs stored in column 2 of Z2.... etc.
Hope this makes sense. I am learning lots with Matlab and appreciate everyones help
scour_man
scour_man el 21 de Jul. de 2011
I think I answered my own question, here is the code I used and it seems to work
a=cell(1,size(Z,2));
b=cell(1,size(Z,2));
for j1=1:size(Z,2),
[a{j1} b{j1}]=armasel(Z(:,j1));
end
Z2=cell(1,size(Z,2));
for j1=1:size(Z,2),
Z2{j1}=arma2pred(a{:,j1},b{:,j1},Z(:,j1),3);
end
Andrei Bobrov
Andrei Bobrov el 21 de Jul. de 2011
%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
scour_man
scour_man el 22 de Jul. de 2011
nice

Iniciar sesión para comentar.

Más respuestas (1)

Jan
Jan el 20 de Jul. de 2011

1 voto

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.

2 comentarios

scour_man
scour_man el 20 de Jul. de 2011
??? Subscripted assignment dimension mismatch.
Error in ==> Untitled at 3
Z2(:,j1)=arma2pred(asel,bsel,Z(:,j1),3)
does this help? thanks for help by the way am learning lots
scour_man
scour_man el 20 de Jul. de 2011
asel and bsel are just the same as a and b

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by