Abdul Majeed
Abdul Majeed

Reputation: 79

Value insertions from Matrix to array cells issue

I have a matrix of values as given below.

candidate_edges =
   10.0000   10.0000    6.0000
  155.5000  105.5000   75.5000
   25.5000  105.5000   75.5000
  295.5000  415.5000  155.5000
  185.5000  415.5000  155.5000
  185.5000  485.5000  155.5000
  195.5000  305.5000   74.5000
  115.5000  305.5000   74.5000
  115.5000  395.5000   74.5000
  195.5000  395.5000   74.5000
   25.5000  185.5000   75.5000
  155.5000  185.5000   75.5000
  295.5000  415.5000    5.5000
  295.5000  415.5000  155.5000
  185.5000  415.5000  155.5000
  195.5000  305.5000    5.5000
  195.5000  305.5000   74.5000
  195.5000  395.5000   74.5000
  295.5000  415.5000    5.5000
  195.5000  395.5000    5.5000
  295.5000  485.5000    5.5000
  300.0000  600.0000         0
  295.5000  415.5000  155.5000
  185.5000  415.5000  155.5000

I want to generate an array with length 1: length(candidate_edges) and in each cell, I want to store each row of the above matrix. Below is my code

cnode = zeros(1, n);
cnode = cell(1, n);
for k = 1:length(cnode)
    for j = 1:length(candidate_edges)
        cnode{k} = candidate_edges(j,:);
    end
end

In the output, I get only same value in each cell.

Upvotes: 0

Views: 36

Answers (2)

ammportal
ammportal

Reputation: 991

You can also use the inbuilt MATLAB function mat2cell for the same like this:

cnode = mat2cell(candidate_edges, ones(1, size(candidate_edges, 1)));

Upvotes: 1

Vahe Tshitoyan
Vahe Tshitoyan

Reputation: 1439

You do not need two loops. e.g. this should do

cnode = cell(1, size(candidate_edges, 1))
for k = 1:numel(cnode)
    cnode{k} = candidate_edges(k,:);
end

Upvotes: 1

Related Questions