averaging images, code doesn't work - matlab

I have this matlab code for creating averages of several images:
cd('C:\images')
all_images=dir('*.jpg');
[sj,xx]=size(all_images);
j=1;
file_name=char(all_images(j).name);
tmp=imread(file_name);
[m,n]=size(tmp);
template=zeros(m,n);
for i=1:length(all_images)
file_name=char(all_images(i).name);
tmp=double(imread(file_name));
template=template+tmp;
end
template=template/length(all_images);
imagesc(template)
imwrite(uint8(template),'template.jpg','jpg')
The case is that I obtained the following error:
Error
using + Matrix dimensions must agree.
Error
in averagetemplate (line 15) template=template+tmp;
Any idea? I have to say that I am newbie in matlab coding.
Thanks

Just change [m,n]=size(tmp) in line 6 to [m,n,~]=size(tmp)and your problem will be solved.
In the below code :
[m,n]=size(tmp)
Matlab is computing n*3 as the column of matrix so you will get the dimension error in next few line.

You are overwriting your temp variable with what appears to be a single value here:
tmp=double(imread(file_name));
and it may be different dimensions M x N regardless. Matlab is saying to add matricies they have to have same number of rows and cols.

Related

Error during rm ANOVA (Matlab) with random factor

I am trying to run a repeated measures ANOVA in Matlab with 4 factors including one factor representing my subjects which I want as a random factor.
The code I have is as follows:
[p,table,stats] = anovan(COORDS_SUBJ_II,{group_hand,group_stim,group_time,group_subs},'random',4,'varnames',{'HAND','STIM','TIME','SUBS'});
Here, all variables have the same dimension, which is 1350x1(all types are 'double'). I checked my code with some proposed code on the net and it matches, yet I keep getting the following error...
Error using chi2inv (line 3)
P and V must be of common size or scalars
Error in anovan>varcompest (line 838)
L = msTerm .* dfTerm ./ chi2inv(1-alpha/2,dfTerm);
Error in anovan>getRandomInfo (line 811)
[varest,varci] = varcompest(ems,randomterms,msTerm,dfTerm,alpha);
Error in anovan (line 296)
getRandomInfo(msterm,dfterm,mse,dfe,emsMat,randomterm,...
My dependent variable (COORDS_SUBJ_II) has a couple of NaN's in it, although I ran the code once where I replaced those NaN's with random numbers and it still gives me the same error. I am kind of lost right now and would appreciate any help.
Best
ty
Found it out. A toolbox I downloaded a while ago also had the chi2inv command and this prompted the error =)

reshape() command causes an error being evoked from a loop

psz=length(pic)
p=0; %masking counter
for i=1:outs:(psz) % dividing in blocks
for j=1:outs:(psz)
p=p+1
blocks(:,:,p)=pic(i:i+outs-1,j:j+outs-1);
ins(:,p)=reshape(blocks(:,:,p)',1,ins')';
end
end
so to begin with i am trying to reproduce sanger rule for pca using neural networks so if somone wants to duscuss about it or give him my code he can message me:)
i get the following error
Error using reshape
To RESHAPE the number of elements must not change.
Error in train (line 30)
ins(:,p)=reshape(blocks(:,:,p)',1,ins')';
Converting blocks(:,:,p) to a column vector should clear the error as long as there are the same number of elements in blocks(:,:,p) as the row length of ins
col_vec = blocks(:,:,p);
ins(:,p) = col_vec(:);
The size of blocks(:,:,p) is outs-by-outs so to make a column vector, it has to be (outs*outs)-by-1. To do this, the command would be:
ins(:,p)=reshape(blocks(:,:,p)',outs*outs,1); % no need for '
However, be sure that size(ins,1) is outs*outs or it won't work. What is the size of ins (and blocks, out of curiosity)? Also, be sure you really want the ' on blocks because the command will work with or without it.

Using a value from a matrix as a multiplier in an equation?

Started using MatLab a couple of weeks ago, I don't know much proper syntax / terminology.
I'm trying to use a value in a 3x1 matrix as a multiplier in an equation later.
This is to draw a circle with radius and centre point defined by values input by the user.
I have a pop-up window, the values are input by the user and stored in this 3x1 cell (labelled answer).
How do I use the second value of that matrix, answer(2), in the following equation:
x = 'answer(2)' * cos(theta) + xCentre;
This error message appears:
Error using .*
Matrix dimensions must agree.
Error in Disks (line 40)
x = 'answer(2)'.* cos(theta) + xCentre;
In MATLAB, apostrophes ('') define a string. If the name of your matrix is answer, you can refer to its second value by the command answer(2) as mentioned by #Schorsch. For more infos on vectors and matrices, you can check this site.
In addition to what the previous answer says, its important to understand what exactly you are doing before you do it. Only add the ('') if you are defining a string, which generally occurs when dealing with variables. In your case, you simply have a matrix, which is not a string, but rather a set of numbers. You can simply do answer(2) as aforementioned, because answer(2) calls up the second value in your matrix while 'answer(2)' has you trying to define some variable that does not exist.
the most important thing is truly understanding what you are doing to avoid the basic syntax errors.

I am not able to figure out how to carry out the Matrices multiplication in matlab

I have to carry out the following operation
R=[0,0.5,-0.25;-0.25,0,0.25;0,0,0.25];
B=[0,k21,k31;k12,0,k32;0,0,k];
G=inv(R).*B;
g=det(G);
but Matlab is showing the following error
??? Error using ==> horzcat
CAT arguments dimensions are not consistent.
Error in ==> g at 60
B=[0,k21,k31;k12,0,k32;0,0,k];
K21,K31,K12,K32 and k all have dimensions of 923334 by 1. Can anyone help me how can I carry out the following operation.
Your code works well for me. Check that the k-values (k12,k31,k32...) are scalars (or 1x1 dimension)
EDIT :
For the case you mention, k's are nx1, one simple way is to perform a loop:
R=[0,0.5,-0.25;-0.25,0,0.25;0,0,0.25];
for ii=1:length(k)
B=[0,k21(ii),k31(ii);k12(ii),0,k32(ii);0,0,k(ii)];
G=inv(R).*B;
g(ii)=det(G);
end
There is also a "vectorized" way to do that, but it seems to be good enough...

error message on the dimensions of matrix in Matlab

When I run this program, I get the error message:
??? Index exceeds matrix dimensions.
Error in ==> if a(1,i)==0
could you tell me why??
a = randi(5,4,100)-ones(4,100);
[n m]=size(a);
for i=1:m
if a(1,i)==0
a(:,i)=[];
end
end
The reason is that you are removing columns from your matrix, so inside the for loop you are reducing its dimension. Then you try to access a column with an index which refers to the original matrix, before the columns were removed.
Try this instead:
a = randi(5,4,100)-1;
ind2remove = (a(1,:) == 0);
a(:,ind2remove) = [];
You get that error because during the execution of this for loop, you might remove some columns. Therefore the dimensions of the matrix will decrease and you will try to access elements that have been moved to a different place.
To do wht you want, you either have to write a while loop, keeping the indices in check manually. The other solution is to vectorize your solution as itamar Katz has shown. That solution is more MATLAB-esque than writig a while loop.
But I have noticed that allowing a random algorithm to emit vectors of random length can sometimes prove more difficult to handle than fixed-length vectors. So you might want to construct your vector in such a way that you don't even have to remove such entries, depending on your application this might be accomplished by generating the first row and other rows with different instructions.