-2

I have an 80x1 cell array, where each cell element has a different size. I want to round the second and third columns of each cell to the closest integer, divided by 4. I have no idea hoe to do it. I have tried cellfun so far but it didint work. See below for my code:

clear all; 
clc;
for k = 1 : 80 
    A{k} = 1 : k; 
end 

for k = 1 : 80 
    B{k} = 1 : k; 
end 

for k = 1 : 80 
    newb{k} = 1 : k; 
end

for k = 1 : 80 
    r5{k} = 1 : k; 

    % code to create Mcell i.e cell array 400 x 1 
    Mcell = mat2cell(a5n,repmat(174,400,1),3)

    %each image size x and y 
    for ii=1:80 [A{ii,1},B{ii,1}] =find(Mcell{ii,1} == 280);

    % ii 
    %find sizes 13 and their locations in Mcell(ii,1)  
    newb{ii,1}=Mcell{ii,1}(A{ii,1},:);

    %ii matrix with size and locations x y. i.e size=13 x=4 y=50 
end 

cellfun(@round,newbii,1}(:,2:3)/4)*4);   

newb{ii,1}=Mcell{ii,1}(A{ii,1},:);
Wolfie
  • 27,562
  • 7
  • 28
  • 55
  • 3
    *what* did you try? Please show your attempts on a minimal excerpt of your data, so that we can play with that too – Rody Oldenhuis Dec 12 '16 at 11:41
  • A simple for loop might be the best solution in this case. – Daniel Dec 12 '16 at 11:42
  • clear all; clc for k = 1 : 80 A{k} = 1 : k; end for k = 1 : 80 B{k} = 1 : k; end for k = 1 : 80 newb{k} = 1 : k; end – Dimitris Pasias Dec 12 '16 at 11:47
  • for k = 1 : 80 r5{k} = 1 : k; ................ code to create Mcell i.e cell array 400 x 1.............................Mcell = mat2cell(a5n,repmat(174,400,1),3)%each image size x and y for ii=1:80 [A{ii,1},B{ii,1}] =find(Mcell{ii,1} == 280);% ii %find sizes 13 and their locations in Mcell(ii,1) – Dimitris Pasias Dec 12 '16 at 11:51
  • newb{ii,1}=Mcell{ii,1}(A{ii,1},:);%ii matrix with size and locations x y. i.e size=13 x=4 y=50 end cellfun(@round,newbii,1}(:,2:3)/4)*4) – Dimitris Pasias Dec 12 '16 at 11:52
  • 1
    @DimitrisPasias For readability, it's better if you **edit** your question with your code instead of writing it as comments. – NLindros Dec 12 '16 at 12:56

2 Answers2

1

This should do your thing:

cellfun(@(x)(round(x(:,[2:3])/4)), C, 'UniformOutput', false)
Yishai E
  • 481
  • 3
  • 9
0

In response to your code, for a start you don't need so many for loops!

Do one 1:80 loop containing all the similar lines...

for k = 1:80

    A{k} = 1 : k; 
    B{k} = 1 : k;
    newB{k} = 1 : k;
    r5{k} = 1 : k;

    % code to create Mcell i.e cell array 400 x 1 
    Mcell = mat2cell(a5n,repmat(174,400,1),3)

    % Not sure what your further lines are trying to achieve
    % so I have stopped copying code here...

end

In answer to your actual question, to "round the values in the second and third columns to the nearest integer divided by 4"...

Be aware that if you're talking about the Cells A, B, etc. then note that some of these don't have two or three columns as your loop goes from 1 so the first entry only has 1 column?

But your initial code aside, you should be able to do the following with an 80x1 Cell called myCell:

for i = 1:80

    % Access myCell{Row}(Column)
    % Where myCell contains 80 row vectors
    myCell{i}(2) = round(myCell{i}(2)) / 4;
    myCell{i}(3) = round(myCell{i}(3)) / 4;

end

It is unclear whether you mean to round before or after the division by 4. The above code rounds first, if you want to round after then of course instead use

myCell{i}(3) = round(myCell{i}(3) / 4 );

Wolfie
  • 27,562
  • 7
  • 28
  • 55