??? In an assignment A(I) = B, the number of elements in B and I must be the same. matlab error

1 visualizzazione (ultimi 30 giorni)
I have problem with indexing
my code is:
function [d] = tazisko6(A)
d1=length(A); d2 =length(A{1});
s=0;
for i=1:d1
for j=1:d2
s=s+(A{i}{j});
end
if d2~=1
d=s/2;
else
d=s;
end
end
[t1,t2]=size(Z)%cell 1x3 cell
for i=1:t2 [d]=tazisko6(Z(i))
end
ouput: d=2.8000 3.6000 17.2000
d= 27.7500 35.3500 50.3000
d =
2.4000 3.5500 17.7500
i want to have ouput in the form d(1)= 2.8000 3.6000 17.2000 d(2)= 2.8000 3.6000 17.2000 d3(3)= 2.4000 3.5500 17.7500
when I add to the source code variables i
for i=1:t2 [d(i)]=tazisko6(Z(i))
end
matlab error ??? In an assignment A(I) = B, the number of elements in B and I must be the same
Can you help me with indexing ?
Thanks
  2 Commenti
Image Analyst
Image Analyst il 2 Nov 2013
You forgot to give the error message. Don't snip or paraphrase. Copy and paste ALL THE RED TEXT so we can tell what is going on. And I don't understand this "when I add to the source code variables i
for i=1:t2 [d(i)]=tazisko6(Z(i))
end"
You're not adding anything to i - it's the same as the first time you do that, and anyway, you're not adding anything to i, you're specifying that it take on a sequence of numbers: 1, 2, 3, 4, ...t2. So I don't see what you did differently in that chunk of code that is different than the first time you called it when it worked and you got d as an output.

Accedi per commentare.

Risposte (2)

Nitin
Nitin il 2 Nov 2013
I think you might need to initialize d here before making the addition

Tomas
Tomas il 2 Nov 2013
this is my code full
clear,clc
MON = [2.8 3.6 17.2; 5.4 8.3 15.8; 2.5 3.2 17.6; 9.9 10.7 13.6; 5.5 8.9 15.5; 9.7 11 13.9; 2.3 3.9 17.9; 5.7 8.1 15.1; 9.4 10.5 13; 9.9 13.2 13.7];
[m,n]=size(MON)
T=input('Zadaj počet typickych bodov :')%uzivatel zada koľko chce mať typickych bodov
for i=1:T %Pociatocne typicke body
TAZ=MON(1:T,:);
end
TAZ
[r,s]=size(TAZ)%rozmery TAZ
Z=cell(1,T)%vytvorenie zhlukov
z=1;
for i=1:m
for j=1:r
B = MON(i,:);
Z1(j)= euklid6(TAZ(j,:),B);%euklidovska vzdialenost
end
Z1
minD = min([Z1])%minimalna hodnota
[i_min,j_min]=find(Z1==minD)%indexacia minimalnej hodnoty
[e,f]=size(Z1)
for h=1:f
if (Z1(h) == Z1(j_min))
Z{h}{z}=B
z=z+1;
end
end
end
Z;
d1=length(Z)
for i=1:d1 %zmaze prazdne miesta
Z{i}(cellfun(@isempty,Z{i})) = [];
end
Z
[t1,t2]=size(Z)
for i=1:t2
d(i)=tazisko6(Z(i))
end
matlab error ??? In an assignment A(I) = B, the number of elements in B and
I must be the same.
Error in ==> knew at 78 d(i)=tazisko6(Z(i))
??? In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in ==> knew at 78 d(i)=tazisko6(Z(i))
function [d] = tazisko6(A)
d1=length(A); d2 =length(A{1});
s=0;
for i=1:d1
for j=1:d2
s=s+(A{i}{j});
end
if d2~=1
d=s/2;
else
d=s;
end
end
I do need each iteration stored in memory, so that I could continue with that work.
Thanks
  1 Commento
Image Analyst
Image Analyst il 2 Nov 2013
It is not complete (full). I get this when I try to run it:
Undefined function 'euklid6' for input arguments of type 'double'.
Error in test3 (line 28)
Z1(j)= euklid6(TAZ(j,:),B);%euklidovska vzdialenost
You did not supply euklid6() and you didn't tell us what number or letters to input at the input prompt. Please make it easy to help you, not hard . http://www.mathworks.com/matlabcentral/answers/6200-tutorial-how-to-ask-a-question-on-answers-and-get-a-fast-answer.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by