Problems with saving vectors to cell array
    조회 수: 5 (최근 30일)
  
       이전 댓글 표시
    
Hello, newbie to Matlab here. I have a problem with saving vectors (which are not of the same length) to cell array. Let´s say I have a 2x8 matrix p and I access it´s cells via linear indices. I create vectors of pixels between p(1) : p(3) then p(5) : p (7) and p(9) : p(11). If I write this (e.g. vec = p(1) : p(3)) into command window I get the vector. What I try is to create these three (or more it depends) vectors and save them in cell array as follows:
rows = 0.5.*length(p);
k = 2.*length(p);
for i = 1 : 4 : k
    for j = 3 : 4 : k
        for m = 1 : rows
            p(i);
            p(j);
            M{m} = p(i) : p(j);
        end
    end
end
The problem is that the M-array created has 3 rows and every row contains the vector of same length (the last vector created, in my case p(9) : p(11)). What is wrong in my code that the output is this and not three rows containing three vectors as I desribed before?
I deal with this long time and finally I really dont know :( Thank you very much for your advices, Mari.
댓글 수: 1
  the cyclist
      
      
 2014년 3월 8일
				
      편집: the cyclist
      
      
 2014년 3월 8일
  
			Can you please give an example of the matrix p, such that the code is self-contained and illustrates the problem?
채택된 답변
  the cyclist
      
      
 2014년 3월 8일
        
      편집: the cyclist
      
      
 2014년 3월 8일
  
      I am not 100% clear on what you want to do, but here is a guess:
p = [1  2  3  4  5  6  7  8;
     9 10 11 12 13 14 15 16];
rows = 0.5.*length(p);
k = 2.*length(p);
rowCounter = 0;
for i = 1 : 4 : k
    rowCounter = rowCounter+1;
    j = i+2;
    p(i);
    p(j);
    M{rowCounter} = p(i) : p(j);
end
The main things that I have done are:
- Introduced some fake p data
- Removed the loop over j, because it seems that j is always determined by i, rather than being its own independent loop
- Removed the loop over m, because again this is really just counting rows, which is effectively done already in the one loop
I think the important thing to realize (if I have understood your intent correctly), is that you really only have one loop -- the starting value of your index -- and everything keys off of that.
I expect I don't have this quite right, but I bet it is closer to what you wanted to do.
추가 답변 (0개)
참고 항목
카테고리
				Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

