# Reshape a matrix without using a for loop 70

Posted by **Doug Hull**,

**Category:**- Format: Video,
- Level: Basic

## 70 CommentsOldest to Newest

**1**of 70

**2**of 70

**3**of 70

**4**of 70

**5**of 70

function d= get_frames(im, startframe) for frame=framefirst:framelast; data = get_frames(handles.im, frames); addframe=0; for ycount=yfirst:ylast; addframe=0; for xcount=xfirst:xlast; while dwellcheck==1; %calculate ~10 parameters (means and stds and such) if spot>=parameter1 & spot>=parameter2 & spot>constantafter this i check to see if the same spot (x,y) in the next frame (data = get_frames(handles.im, frames+addframe);) also meets these parameters. when checked all frames, then save spot and go back to searching for next x,y spot. then go to next startframe. Any advice? Or am I stuck with the speed? Thanks a lot!

**6**of 70

**7**of 70

accstdev = std([datamatrixacc(1,1:5)... datamatrixacc(2,1) datamatrixacc(2,5)... datamatrixacc(3,1) datamatrixacc(3,5)... datamatrixacc(4,1) datamatrixacc(4,5)... datamatrixacc(5,1:5)]); %takes up 32% of the time donstdev = std([datamatrixdonor(2, 2:4) datamatrixdonor(3,2:4) datamatrixdonor(4,2:4)]); %takes up 30% of the time spotmean3 = mean2(datamatrixacc(2:4, 2:4)); %each of these take up about 5% of the time spotmean4 = mean2(datamatrixacc(1:5, 1:5)); spotmean1 = mean2(datamatrixdonor(2:4, 2:4)); spotmean2 = mean2(datamatrixdonor(1:5, 1:5));I saved a lot of time by creating a new 5x5 matrix around each spot and accessing that instead of the entire 512x512 matrix. Any other recommendations? Thanks again.

**8**of 70

**9**of 70

**10**of 70

**11**of 70

**12**of 70

**13**of 70

**14**of 70

**15**of 70

**16**of 70

**17**of 70

**18**of 70

**19**of 70

**20**of 70

**21**of 70

**22**of 70

**23**of 70

**24**of 70

**25**of 70

**26**of 70

**27**of 70

**28**of 70

**29**of 70

**30**of 70

**31**of 70

a = [1 2; 3 4] A(:,:,1) = a A(:,:,2) = a+4 A(:,:,:,2) = A + 10 reshape(A,[2,8]) a = 1 2 3 4 A = 1 2 3 4 A(:,:,1) = 1 2 3 4 A(:,:,2) = 5 6 7 8 A(:,:,1,1) = 1 2 3 4 A(:,:,2,1) = 5 6 7 8 A(:,:,1,2) = 11 12 13 14 A(:,:,2,2) = 15 16 17 18 ans = 1 2 5 6 11 12 15 16 3 4 7 8 13 14 17 18

**32**of 70

**33**of 70

a = [11 1; 12 1; 23 2; 24 2] target = 2; [vr, vc] = find(a(:,2) == target); newMatrix = a(vr, :)

**34**of 70

**35**of 70

**36**of 70

**37**of 70

**38**of 70

>> a = magic(6) a = 35 1 6 26 19 24 3 32 7 21 23 25 31 9 2 22 27 20 8 28 33 17 10 15 30 5 34 12 14 16 4 36 29 13 18 11 >> b = a(1:3,1:4) b = 35 1 6 26 3 32 7 21 31 9 2 22

**39**of 70

for u=1:13 c=0; for i=i1:i2 for j=j1:j2 a=I3(i,j); if (a==u) x=[i,j]; c=k(norm((x-center)./dim)^2)+c; end end end q(u)=c/f; endwould you please give me an obvious answer

**40**of 70

**41**of 70

**42**of 70

**43**of 70

**44**of 70

**45**of 70

**46**of 70

**47**of 70

**48**of 70

**49**of 70

**50**of 70

**51**of 70

A(:,:,1)=ones(3)*x(1); A(:,:,2)=ones(3)*x(2); A(:,:,6)=ones(3)*x(3);Doug

**52**of 70

**53**of 70

**54**of 70

x=[2 4 6] for k=1:numel(x) A(:,:,k)=ones(numel(x)).*x(k); endwithout the loop. Daniel

**55**of 70

**56**of 70

**57**of 70

**58**of 70

**59**of 70

**60**of 70

A = 1:4; B = reshape(A,2,2) C = B(:,[1 1 2 2])--Loren

**61**of 70

**62**of 70

**63**of 70

**64**of 70

**65**of 70

**66**of 70

**67**of 70

**68**of 70

**69**of 70

**70**of 70

## Recent Comments