Selecting element at same position in a matrix

1 visualizzazione (ultimi 30 giorni)
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?

Risposta accettata

Jan
Jan il 13 Apr 2021
Modificato: Jan il 13 Apr 2021
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 Commento
Joel Schelander
Joel Schelander il 13 Apr 2021
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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su MATLAB in Help Center e File Exchange

Tag

Prodotti


Release

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by