필터 지우기
필터 지우기

Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.

조회 수: 1 (최근 30일)
Buenas tardes en el codigo que estoy estudiando cuando lo trnascribi sale el error:
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
Error in ejercicio_15_3_4 (line 83)
indices(i)= find(coordenadas_ascendentes(i) == coordenadas_x);
el código es el siguiente:
clc
clear;
x=0:0.01:40;
k=(14-1.2*x)/2.25;
g=(8-x)/1.1;
h=9-2.5*x;
z= @(X,Y) 1.75*X + 1.25*Y;
plot(x,k,x,g,x,h);
hold on;
grid;
title('Maximización')
xlabel x;
ylabel y(x);
%Generación de al intersección en el eje y
ordenada= [ k(1) g(1) h(1)];
miny=min(ordenada);
plot(x(1),miny, 'o');
%Generar intersección del eje x
x1=find(k==0);
x2=find(g==0);
x3=find(h==0);
ejex=[x(x1) x(x2) x(x3)];
minx=min(ejex);
plot(min(ejex),0,'o');
xlim([0, minx+3]);
ylim([0, miny+3]);
%Secuencia para graficar la intersección entre las funciones
f(1,:)=k;
f(2,:)=g;
f(3,:)=h;
combinaciones=nchoosek(1:3,2);
cant_comb=size(combinaciones);
cant_comb=cant_comb(1);
c=1;
for i=1:cant_comb
CUMPLE=1;
comb_temp=combinaciones(i,:);
f_uno=f(comb_temp(1),:);
f_dos=f(comb_temp(2),:);
if isempty(find(f_uno==f_dos))
intersecciones(i,:)=find(f_uno==max(f_uno));
else
intersecciones(i,:)=find((f_uno==f_dos));
end
inter_temp=intersecciones(i);
for j=1:3
if f_uno(inter_temp)>f(j,inter_temp)
CUMPLE=0;
end
end
if CUMPLE==1
inter_validas(c,1)=inter_temp;
inter_validas(c,2)=comb_temp(1);
c=c+1;
end
end
%Intersecciones válidas
cant_inter_validas= size(inter_validas);
cant_inter_validas=cant_inter_validas(1);
for i=1:cant_inter_validas
coordenadas(i,:)=[x(inter_validas(i,1)),f(inter_validas(i,2),inter_validas(i,1))];
plot(coordenadas(i,1), coordenadas(i,2),'o');
end
coordenadas_x=coordenadas(:,1);
coordenadas_ascendentes=sort(coordenadas_x);
coordenadas_finales(:,1)=coordenadas_ascendentes;
for i=1:length(coordenadas_ascendentes)
indices(i)= find(coordenadas_ascendentes(i) == coordenadas_x);
coordenadas_finales(i,2)= coordenadas(indices(i),2);
end
%Polígono
PX=[0,x(1), (coordenadas_finales(:,1))',minx];
PY=[0,miny, (coordenadas_finales(:,2))',0];
%Gráfico del polígono
fill(PX,PY,'r');
z1=z;
for i=1:length(PX)
X=PX(i);
Y=PY(i);
valorz(i)=z1(X,Y);
end
indicezmax=find(valorz==max(valorz));
coordenadamaxz=[PX(indicezmax) PY(indicezmax)];
plot (PX(indicezmax), PY(indicezmax),'o','MarkerSize',10);
str=strcat('El óptimo se alcanza en :',num2str(PX(indicezmax)),',',num2str(PY(indicezmax)));
text1=text(minx-3,miny-3,str);
str=strcat('Y tiene un valor de:',num2str(max(valorz)));
text2=text(minx-3,miny-3,miny-1,str);
  댓글 수: 2
Dyuman Joshi
Dyuman Joshi 2022년 11월 23일
find() returns all the indices that satisfy the condition tested.
y=[1 3 2 0 2 1 4 2 0 2];
i=find(y==y(3))
i = 1×4
3 5 8 10
When a condition is satisfied more than once, it will return multiple (corresponding) indices.
You can not assign multiple values to a single element of an array.
You can, however, get the n index/indices from first or last that satisfies the condition -
%1 from first
i1=find(y==y(3),1)
i1 = 3
%2 from last
il=find(y==y(3),2,'last')
il = 1×2
8 10
Voss
Voss 2022년 11월 23일
To add on to what @Dyuman Joshi correctly points out, if find in this line
indices(i)= find(coordenadas_ascendentes(i) == coordenadas_x);
returns anything other than a single index, you'll get an error.
In particular, it may be that no element of coordenadas_x is equal to coordenadas_ascendentes(i), in which case find returns an empty array.

댓글을 달려면 로그인하십시오.

답변 (1개)

Sanjana
Sanjana 2023년 6월 7일
Hi Marcos,
I understand that there is a mismatch in the dimensions of the variables in the assignment operation. The problem here is at this line,
indices(i)= find(coordenadas_ascendentes(i) == coordenadas_x);
For “==” operation, the arrays should have same dimensions, please modify the below lines,
for i=1:length(coordenadas_ascendentes)
indices(i)= find(coordenadas_ascendentes(i) == coordenadas_x);
coordenadas_finales(i,2)= coordenadas(indices(i),2);
end
to,
indices= find(coordenadas_ascendentes == coordenadas_x);
coordenadas_finales(:,2) = coordenadas(indices,2);
Hope this helps!

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by