How can I make following part of code without for loop?
Show older comments
How can I make following part of code without for loop?:
for i=1:n_values
pre_matrix(i,J{i})=0
end
J{i} is a cell array that each cell includes some numbers that I want to change those elements in i-th row of pre_matrix. Each cell has different numbers and size of cells are different.
pre_matrix is very huge matrix, therefore it takes so long time to create matrix everytime. Is it possible to do same work without for loop?
Accepted Answer
More Answers (1)
Rik
on 13 May 2020
Your issue is probably not with the loop itself, but with the modifications to the large matrix. You should probably generate a list of indices and do the modification in one go.
sz=size(pre_matrix);
ind_list=cell(1,n_values);
for i=1:n_values
ind_list{i}=sub2ind(sz,i*ones(size(J{i})),J{i});
end
ind_list=cell2mat(ind_list);%untested code, check this line
pre_matrix(ind_list)=0;
1 Comment
Shamsi Musayev
on 13 May 2020
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!