Selecting element at same position in a matrix

1 回表示 (過去 30 日間)
Joel Schelander
Joel Schelander 2021 年 4 月 13 日
コメント済み: Joel Schelander 2021 年 4 月 13 日
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 Center および File ExchangeMATLAB についてさらに検索

タグ

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by