cobinning values in an array

function [hor vrt dgr dgl] = matbrkup(M) [x,y] = size(M); for i = 1:x for j = 1:y z = cell2mat(M(i,j)); n = sum(z); m = sum(z,2); [s,d] = spdiags(z); s1 = sum(s); [t,d] = spdiags(z'); t1 = sum(t); a = max(n); b = max(m); c = max(t1); d = max(s1); if (a > length(z)/3) vrt = 1; else vrt = 0; end; if (b > length(z)/3) hor = 1; else hor = 0; end; if (c > length(z)/3) dgr = 1; else dgr = 0; end; if (d > length(z)/3) dgl = 1; else dgl = 0; end; w = cat(2,hor,vrt,dgr,dgl) end; end;
y = matbrkup(x);
w =
1 1 0 0
w =
1 0 0 0
w =
0 0 0 0
w =
1 1 0 0
w =
1 0 0 0
w =
0 0 0 0
w =
0 1 0 0
w =
0 0 0 0
w =
0 0 0 0
i want all the values of w to be combined into on array and im not very familiar with matlab and i tried using the cat function but im not sure why it doesnt work or where to go from here..

1 Commento

http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup

Risposte (1)

Andrei Bobrov
Andrei Bobrov il 5 Dic 2011
in your case:
w{i,j} = cat(2,hor,vrt,dgr,dgl);
variant
% e.g. data:
M = reshape(arrayfun(@(i1){rand(randi([3 8]),randi([4 10]))},1:6,'un',0),3,[]);
% solution:
y = cellfun(@(x)cell2mat(x),M,'un',0);
out1 = cellfun(@(x)[max(sum(x)),max(sum(x,2)),...
max(sum(spdiags(x))) max(sum(spdiags(x(:,end:-1:1))))],y,'un',0);
k = cellfun(@length,y,'un',0);
out = cellfun(@(a,b)a>b/3,out1,k,'un',0);
variant with loop for..end
out3 = cell(size(M));
for i1 = 1:numel(M)
a = cell2mat(M{i1});
b = max(sum(a));
c = max(sum(a,2));
d = max(sum(spdiags(a)));
r = max(sum(spdiags(a(:,end:-1:1))));
out3{i1} = [b c d r] > length(a)/3;
end

Questa domanda è chiusa.

Tag

Richiesto:

ben
il 5 Dic 2011

Chiuso:

il 20 Ago 2021

Community Treasure Hunt

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

Start Hunting!

Translated by