Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do i create a loop for this?

280 views
Skip to first unread message

Xiao

unread,
Jan 27, 2009, 12:26:02 AM1/27/09
to
Hi all,

I have a question as follow:

for example:

network = [] [1;0] [] ;
[] [] [0;1] ;
[] [] [1;0] ;
[] [] [] ;
[] [] [] ],

First, i need to check through the columns to see if there is more than one item in it. Based on the network, col 3 has more than one item. After which, i need to store the positions of the 2 items.

that means to say,

When more than 1 item in a column
store those positions [network (2,3) & network (3,3)] in an ARRAY.

I am using, [I, J] = ind2sub (size(network), CONDITION)
but what is the condition i need to set, so that it can store the 2 positions (network(2,3) & network(3,3))?

Thank you.

us

unread,
Jan 27, 2009, 1:31:02 AM1/27/09
to
"Xiao "

> network = [] [1;0] [] ;
> [] [] [0;1] ;
> [] [] [1;0] ;
> [] [] [] ;
> [] [] [] ],
> When more than 1 item in a column
> store those positions [network (2,3) & network (3,3)] in an ARRAY.

your example is not quite clear (why is network(1,2) not used?)...

one of the solutions

% the data
c={
[],[1,2],[]
[],[],[3,4]
};
% the engine
[ir,ic]=find(~cellfun(@isempty,c));
% the result
disp([ir,ic]);
%{
% row / col
1 2
2 3
%}

us

Xiao

unread,
Jan 27, 2009, 1:51:01 AM1/27/09
to
Hi,

only when the number of items in a column exceed 1, then i store the position of those involved items. hence, network (1,2) is not used. In the example, only column 3 has more than 1 item, hence i need to store those address [network (2,3) & network (3,3)].

I hope this is clear enough..

Do help me out.

Thank you.

"us " <u...@neurol.unizh.ch> wrote in message <glm9n6$q2j$1...@fred.mathworks.com>...

Xiao

unread,
Jan 28, 2009, 10:17:01 AM1/28/09
to
Hi all,

I tried using the solution provided. However, it doesnt fulfill the requirement that I have mentioned earlier.

What I need is (When Number of items in each column> 1, store those address that belong to the column)

I tried using ind2sub also. But i cant get it right..

What i did is as follows:

[I, J] = ind2sub(size (network), find(Number of items in each col > 1)),

Can anyone have a solution for this?

Waiting for your reply.
Thank you.

"Xiao " <viva_p...@hotmail.com> wrote in message <glmasl$dd0$1...@fred.mathworks.com>...

us

unread,
Jan 28, 2009, 10:42:02 AM1/28/09
to
"Xiao "
> only when the number of items in a column exceed 1, then i store the position of those involved items. hence, network (1,2) is not used. In the example, only column 3 has more than 1 item, hence i need to store those address [network (2,3) & network (3,3)]...

one of the solutions

% the data
c={
[],[],[],[]
[],[1,2],[],[-1,-2]
[],[],[3,4],[-3,-4]
[],[],[5,6],[]
};
% the engine
ix=sum(~cellfun(@isempty,c))<=1;
c(:,ix)={[]};
[rx,cx]=find(~cellfun(@isempty,c));
% the result
disp([rx,cx]);
%{
% row#/col#
3 3
4 3
2 4
3 4
%}

us

Xiao

unread,
Jan 28, 2009, 11:13:01 AM1/28/09
to
Hi.

Thank you.

Is it possible to perform an addition of the content (content of row 3 col 3 + content of row 4 col 3) and display the results as well ?

Where should i edit the program?

Waiting for your reply.

Thank you for your help.

"us " <u...@neurol.unizh.ch> wrote in message <glpuca$drc$1...@fred.mathworks.com>...

us

unread,
Jan 28, 2009, 11:28:03 AM1/28/09
to
"Xiao"
> Is it possible to perform an addition of the content (content of row 3 col 3 + content of row 4 col 3) and display the results as well...

the LAST one of the solutions...

% the data
c={
[],[],[],[]
[],[1,2],[],[-1,-2]
[],[],[3,4],[-3,-4]
[],[],[5,6],[]
};
% the engine
ix=sum(~cellfun(@isempty,c))<=1;
c(:,ix)={[]};
[rx,cx]=find(~cellfun(@isempty,c));

cu=unique(cx);
r=arrayfun(@(x) sum([c{:,x}]),cu);
% the result
disp(r);
%{
18 % col# 3
-10 % col# 4
%}

us

0 new messages