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)
%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]
z1 = 4×9
2.6000 1.6000 0.6000 1.6000 0.4000 0 0.4000 0 0 0.6000 4.8000 4.0000 0.4000 0.2000 0 0 0 0.2000 1.8000 0.4000 1.6000 0.2000 1.2000 0 0.4000 0.6000 0.2000 0 0 0.2000 0 0.2000 1.0000 0 0.2000 1.4000
z2 = [I1;I2;I3;I4]
z2 = 4×9
1 2 4 4 4 8 3 3 7 4 6 3 9 4 6 7 1 5 3 3 5 4 5 3 7 8 3 5 1 5 4 7 7 5 6 9
  2 Comments
Torsten
Torsten on 30 Dec 2022
Edited: Torsten on 30 Dec 2022
Others in the forum might understand what you want - I don't.
Could you give a small example ?
Sarah Bassiouny
Sarah Bassiouny on 30 Dec 2022
in z1 for the first coloumn the smallest value is zero, so for the same index in z2 the value is 5, so that is the value i want to display. for coloumn 2 the value i want to display is 1, based on the minumum value in z1 for that coloumn.
Hope i made it clearer.

Sign in to comment.

Accepted Answer

Voss
Voss on 30 Dec 2022
% two matrices with the same size:
Matrix1 = magic(5)
Matrix1 = 5×5
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
Matrix2 = randi(50,[5 5])
Matrix2 = 5×5
27 10 20 47 45 18 35 33 17 6 43 29 37 9 26 15 2 17 49 11 38 19 3 34 50
% find the index of the minimum element in each column of Matrix1:
[~,idx] = min(Matrix1,[],1)
idx = 1×5
3 2 1 5 4
% show the values of the corresponding elements in Matrix2:
[nrow,ncol] = size(Matrix2);
Matrix2(sub2ind([nrow ncol],idx,1:ncol))
ans = 1×5
43 35 20 34 11
  4 Comments

Sign in to comment.

More Answers (1)

Torsten
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]
z1 = 4×9
2.6000 1.6000 0.6000 1.6000 0.4000 0 0.4000 0 0 0.6000 4.8000 4.0000 0.4000 0.2000 0 0 0 0.2000 1.8000 0.4000 1.6000 0.2000 1.2000 0 0.4000 0.6000 0.2000 0 0 0.2000 0 0.2000 1.0000 0 0.2000 1.4000
z2 = [I1;I2;I3;I4]
z2 = 4×9
1 2 4 4 4 8 3 3 7 4 6 3 9 4 6 7 1 5 3 3 5 4 5 3 7 8 3 5 1 5 4 7 7 5 6 9
[~,I] = min(z1,[],1)
I = 1×9
4 4 4 4 2 1 2 1 1
z2_min = arrayfun(@(i)z2(I(i),i),1:size(z2,2))
z2_min = 1×9
5 1 5 4 4 8 7 3 7

Tags

Community Treasure Hunt

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

Start Hunting!