How to find the distance between vectors whose coordinates are stored in different arrays.

Hi,
I get the error:??? Cell contents assignment to a non-cell array object. at this line in my code:
for k=1:N
A{k}=zeros(length(m),2);
end
That's my first problem. Assuming that I figure out how to solve my first problem, I have another problem: once I have stored values in A{N}, I would like to compute
A{1}(1,:)-A{2}(1,:), A{1}(2,:)-A{2}(2,:), A{1}(3,:)-A{2}(3,:), ..., A{1}(n,:)-A{2}(n,:)
A{2}(1,:)-A{3}(1,:), A{2}(2,:)-A{3}(2,:), A{2}(3,:)-A{3}(3,:), ... , A{2}(n,:)-A{3}(n,:)
A{3}(1,:)-A{4}(1,:), A{3}(2,:)-A{4}(2,:), A{3}(3,:)-A{4}(3,:), ... , A{3}(n,:)-A{4}(n,:)
.
.
.
A{N-1}(1,:)-A{N}(1,:), A{N-1}(2,:)-A{N}(2,:), A{N-1}(3,:)-A{N}(3,:),..., A{N-1}(n,:)-A{N}(n,:)
and store these values into an n X N matrix.
I know I'm gonna find some help here, I always do.
Please help me solve my two problems.
Thanks.

1 Comment

Your example doesn't lead to an n X N matrix. Each A{i}(j,:) is a 1x2 vector, so this leads to a N-1 x 2*n matrix.

Sign in to comment.

Answers (1)

Your first problem.
A=cell(N,1);
for k=1:N
A{k}=zeros(length(m),2);
end

5 Comments

Your 2nd problem
f=@(c) reshape(c.',1,[]);
A=cell2mat(cellfun(f,A(:),'uni',0));
T=speye(N-1,N);
T(N:N:end)=-1;
result = reshape( (T*A).', 2,[]);
result = sqrt(sum(result.^2,1));
result = reshape(result,n,[]);
Thank you very much Matt. There are really few of you guys around. I do thank you for your time.
I get a bunch of zeros in result. I shouldn't have that. Below is the full code.
I am sure the error is where I used
rgb=imread('test_image_%d.png', N);
to read the saved images.
How can I read the saved images?
clc
[SigOrg, fs]=wavread('SpeakerO.wav');
fL = 30; % filter low cutoff freqency
fH = 800; % filter high cutoff frequency
FiltOrd = 4; % filter order
[bl,al]=butter(FiltOrd,[fL fH]/(fs/2)); %creating the coefficients for a bandpass filter with low freq. fL and high freq. fH
sig=filter(bl,al,SigOrg); %filtering out frequencies below 30 and above 600 Hz from the signal
sigCorr = xcorr(sig);
[A,K] = findpeaks(sigCorr); % find the peaks and their locations in the cross correlation
[max1 indx1] = max(A); % find the highest peak and its location among the peaks of the cross correlation
loc1 = K(indx1); % loc1 is the location of the highest peak in the % cross correlation
[A2, K2] = findpeaks(A); % find the peaks and their location among the peaks of the cross correlation's peaks
[max2 indx2] = max(A2);
loc2 = K2(indx2+1);
loc3 = K(loc2);
Pitch = fs/(loc3-loc1);
Num=floor(length(SigOrg)/(loc3-loc1));
x=1;
for N=1:Num
imtool close all;
set(gcf,'Visible','off');
plot(SigOrg(x:N*(loc3-loc1)))
x=N*(loc3-loc1);
filename = sprintf('test_image_%d.png', N);
saveas(gcf, filename, 'png')
end
A=cell(1,Num);
for N=1:Num
rgb=imread('test_image_%d.png', N);
imshow(rgb)
r=rgb(:,:,1);
g=rgb(:,:,2);
b=rgb(:,:,3);
[i j] = find(r==0 & g==0 & b==255);
Counts = accumarray(j,1);
Counts=Counts(Counts~=0);
length(Counts)
Vec=accumarray(j,i);
Vec=Vec(Vec~=0);
m=floor(Vec./Counts);
n=j(1):j(end)
A{N}=zeros(length(m),2);
length(m);
length(n);
A{N}(:,1)=n;
A{N}(:,2)=m;
end
clc
f=@(c) reshape(c.',1,[]);
A=cell2mat(cellfun(f,A(:),'uni',0));
T=speye(Num-1,Num);
T(Num:Num:end)=-1;
result = reshape( (T*A).', 2,[]); % --------Please what does this do?
result = sqrt(sum(result.^2,1)); % contains the distance between vectors in the same position but different figure.
result = reshape(result,length(n),[])% ------Please what does this do?
end
Ok, I think that I figured out how to read the saved images. I applied the following:
rgb=cell(1,Num);
for N=1:Num
imtool close all;
set(gcf,'Visible','off');
plot(SigOrg(x:N*(loc3-loc1)))
x=N*(loc3-loc1);
filename = sprintf('test_image_%d.png', N);
saveas(gcf, filename, 'png')
rgb{N}=imread(filename);
imshow(rgb{N})
end
My problem is that it does not display imshow. It says the following when I type, say, imshow(rgb{4}):
Warning: Image is too big to fit on screen; displaying at 50% > In imuitools\private\initSize at 73 In imshow at 262
Also, the code runs into the following error:
??? Error using ==> cat CAT arguments dimensions are not consistent.
Error in ==> cell2mat at 85 m{n} = cat(1,c{:,n});
Error in ==> ImagExperiment at 55 A=cell2mat(cellfun(f,A(:),'uni',0));
Thank you Matt, You help me a lot.
Would anybody else assist me, please?
Thanks.
Errors like these are a job for the debugger. Set a breakpoint at line 55 in ImagExperiment and rerun your code. When the code halts at line 55, run
z=cellfun(@(c) size(c), A,'uni',0); z{:}
at the K>> prompt and show us the output.
Hi Matt, I found a way around this. And yes, I thank you for your great help. Now, I am experiencing other issues with my code.

Sign in to comment.

Asked:

on 11 Mar 2013

Community Treasure Hunt

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

Start Hunting!