I have to output results (z1 and z2) so the minimum value in Z1 I want to find the value in the same index in Z2, they are 4 rows and 9 columns.
1 view (last 30 days)
Show older comments
%the output of the code are 2 tables z1 and z2, each consists of 4 rows and 9 coloumns, so if the the minimum value in z1 coloumn 1 is 0.2, for the same index in z2 i want to show the value.
x1 = [-56.6 -66.4 -70.8 -69.8 -71 -74.6 -74 -74.4 -73.2];
y1 = [-59.2 -68 -74.4 -71.4 -74.4 -73 -73.2 -74.6 -77];
for w=1:length(x1(:,1))
for k = 1:length(x1(w,:))
for l=1:length(y1(w,:))
dif(l,k) = abs(x1(k) - y1(l));
end
end
[M1,I1]=min(dif(:,:));
end
x2 = [-74 -65.8 -61 -75.4 -73.6 -70.6 -80 -74.8 -72.8];
y2 = [-74.8 -70.8 -57 -73.4 -73 -70.6 -80 -76 -75.8];
for w=1:length(x2(:,1))
for k = 1:length(x2(w,:))
for l=1:length(y2(w,:))
dif(l,k) = abs(x2(k) - y2(l));
end
end
[M2,I2]=min(dif(:,:));
end
x3 = [-73.4 -75.6 -72.8 -70.8 -72.4 -75.2 -62.4 -69.4 -75.4];
y3 = [-78.4 -76.6 -75.2 -70.6 -71.2 -76.8 -62.8 -70 -75.2];
for w=1:length(x3(:,1))
for k = 1:length(x3(w,:))
for l=1:length(y3(w,:))
dif(l,k) = abs(x3(k) - y3(l));
end
end
[M3,I3]=min(dif(:,:));
end
x4 = [-74.8 -76.4 -75 -74.6 -73.2 -72.4 -74.8 -71.4 -62.8];
y4 = [-76.4 -77.2 -78.2 -74.6 -74.8 -71.2 -73.4 -64.6 -61.4];
for w=1:length(x4(:,1))
for k = 1:length(x4(w,:))
for l=1:length(y4(w,:))
dif(l,k) = abs(x4(k) - y4(l));
end
end
[M4,I4]=min(dif(:,:));
end
z1 = [M1;M2;M3;M4]
z2 = [I1;I2;I3;I4]
2 Comments
Accepted Answer
Voss
on 30 Dec 2022
% two matrices with the same size:
Matrix1 = magic(5)
Matrix2 = randi(50,[5 5])
% find the index of the minimum element in each column of Matrix1:
[~,idx] = min(Matrix1,[],1)
% show the values of the corresponding elements in Matrix2:
[nrow,ncol] = size(Matrix2);
Matrix2(sub2ind([nrow ncol],idx,1:ncol))
4 Comments
More Answers (1)
Torsten
on 30 Dec 2022
%the output of the code are 2 tables z1 and z2, each consists of 4 rows and 9 coloumns, so if the the minimum value in z1 coloumn 1 is 0.2, for the same index in z2 i want to show the value.
x1 = [-56.6 -66.4 -70.8 -69.8 -71 -74.6 -74 -74.4 -73.2];
y1 = [-59.2 -68 -74.4 -71.4 -74.4 -73 -73.2 -74.6 -77];
for w=1:length(x1(:,1))
for k = 1:length(x1(w,:))
for l=1:length(y1(w,:))
dif(l,k) = abs(x1(k) - y1(l));
end
end
[M1,I1]=min(dif(:,:));
end
x2 = [-74 -65.8 -61 -75.4 -73.6 -70.6 -80 -74.8 -72.8];
y2 = [-74.8 -70.8 -57 -73.4 -73 -70.6 -80 -76 -75.8];
for w=1:length(x2(:,1))
for k = 1:length(x2(w,:))
for l=1:length(y2(w,:))
dif(l,k) = abs(x2(k) - y2(l));
end
end
[M2,I2]=min(dif(:,:));
end
x3 = [-73.4 -75.6 -72.8 -70.8 -72.4 -75.2 -62.4 -69.4 -75.4];
y3 = [-78.4 -76.6 -75.2 -70.6 -71.2 -76.8 -62.8 -70 -75.2];
for w=1:length(x3(:,1))
for k = 1:length(x3(w,:))
for l=1:length(y3(w,:))
dif(l,k) = abs(x3(k) - y3(l));
end
end
[M3,I3]=min(dif(:,:));
end
x4 = [-74.8 -76.4 -75 -74.6 -73.2 -72.4 -74.8 -71.4 -62.8];
y4 = [-76.4 -77.2 -78.2 -74.6 -74.8 -71.2 -73.4 -64.6 -61.4];
for w=1:length(x4(:,1))
for k = 1:length(x4(w,:))
for l=1:length(y4(w,:))
dif(l,k) = abs(x4(k) - y4(l));
end
end
[M4,I4]=min(dif(:,:));
end
z1 = [M1;M2;M3;M4]
z2 = [I1;I2;I3;I4]
[~,I] = min(z1,[],1)
z2_min = arrayfun(@(i)z2(I(i),i),1:size(z2,2))
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!