# Selecting element at same position in a matrix

3 views (last 30 days)
Joel Schelander on 13 Apr 2021
Commented: Joel Schelander on 13 Apr 2021
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 on 13 Apr 2021
Edited: Jan on 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};
Joel Schelander on 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

### Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

R2017b

### Community Treasure Hunt

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

Start Hunting!

Translated by