Selecting element at same position in a matrix

1 次查看(过去 30 天)
VID1 is a 3x3 cell. I choose a value randomly from the cell like thus.
ID1=VID1{ randi([1,size(VID1,1)],1), randi([1,size(VID1,2)],1 )};
If I have randomly chosen element VID{1,1}. I want to choose an element in the same position in another cell Vehicle1 (3x3 cell). How can I do this?

采纳的回答

Jan
Jan 2021-4-13
编辑:Jan 2021-4-13
If you need the indices again, store them in variables:
i1 = randi([1, size(VID1, 1)]);
i2 = randi([1, size(VID1, 2)]);
ID1 = VID1{i1, i2};
V1 = Vehicle1{i1, i2};
It might be easier to use linear indices:
index = randi([1, numel(VID1)]);
ID1 = VID1{index};
V1 = Vehicle1{index};
  1 个评论
Joel Schelander
Joel Schelander 2021-4-13
Hi @Jan thank you, that helps! However now I get different values compared to my previous method. Was my previous method wrongly stated? I thought the result INCREASE2 (I later save INCREASE2 in a cell) should contain the same values as before.
Just asking if you can see something obvious
Before:
for jj=1:numel(VID1)
V11=Vehicle1{jj};
ID1=VID1{jj};
for jj2=1:numel(VID2)
V22=Vehicle2{jj2};
ID2=VID2{jj2};
%Removes all doubles
if numel(intersect(ID1,ID2))
continue
end
INCREASE2{jj,jj2}=max(HH2+V11+V22)./max(HH2);
IDcell2{jj,jj2}=[ID1 0 ID2];
end
end
After:
for o=1:numel(INCREASE2)
for oz=1:999
k1 = randi([1, size(VID1, 1)]);
kk1 = randi([1, size(VID1, 2)]);
ID1 = VID1{k1, kk1};
V1 = Vehicle1{k1, kk1};
k2 = randi([1, size(VID2, 1)]);
kk2 = randi([1, size(VID2, 2)]);
ID2 = VID2{k2, kk2};
V2 = Vehicle1{k2, kk2};
if numel(intersect(ID1,ID2))
continue
else
IDCELL{o}={ID1; ID2};
INCREASE2{o}=max(HH2+V1+V2)./max(HH2);
break
end
end
end

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by