Info

This question is closed. Reopen it to edit or answer.

How to optimize this code?Please help me and guide me in this direction.

1 view (last 30 days)
hist11=zeros(256,1); hist12=zeros(256,1); hist13=zeros(256,1); hist21=zeros(256,1); hist22=zeros(256,1); hist23=zeros(256,1); hist31=zeros(256,1); hist32=zeros(256,1); hist33=zeros(256,1); using for loop ?Is there any other approach other than for loop?Please help me and guide me in this direction.
  4 Comments
Adam
Adam on 20 Feb 2015
Adding words like "as soon as possible" is more likely to turn people away from answering your question than get people to hurry up!
John D'Errico
John D'Errico on 20 Feb 2015
Yes. The "as soon as possible" addition usually turns me off, as it tends to imply that our time is of less value than the person asking the question.

Answers (3)

John D'Errico
John D'Errico on 20 Feb 2015
Or a 3-d array, if your goal is to have them in the same shape as your numbered array names have them?
allHists = zeros(256,3,3);
Now, when you wanted to access hist13, you just use
allHists(:,1,3)
The nice things is all of your arrays are in one place. You can operate on them as an entire array. You can create arrays with many such histograms in them. And you can even access them programmatically, in a loop, for example.

Elias Gule
Elias Gule on 20 Feb 2015
Edited: Elias Gule on 20 Feb 2015
To store matrices in a matrix you need to use a cell array. A cell array is capable of storing any data type. Hence we are first going to create a 3 x 3 cell array of empty cells; then in each empty cell, we will place a cell array containing a matrix of 256 x 1 zeros; If we tried to put the matrices in a single matrix we would end up with a concatenation of these matrices to a single 768 x 768 matrix.
%% Code begins
hist_ = cell(3,3);
hist_(1:end) = {zeros(256,1)};

Image Analyst
Image Analyst on 20 Feb 2015
Why not use a simple 2d numerical array:
theHists = zeros(256, 9);
  1 Comment
Elias Gule
Elias Gule on 20 Feb 2015
Edited: Elias Gule on 20 Feb 2015
this will only create a matrix of size 256 x 9; if you type hist(1), you get 0 as an answer; which is not what is required. what he/she wanted was a matrix 3 by 3 matrix whose items are each 256 by 1 column vectors.

Products

Community Treasure Hunt

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

Start Hunting!