Create a incremented 2D matrix

10 Ansichten (letzte 30 Tage)
Midimistro
Midimistro am 12 Apr. 2016
Kommentiert: Guillaume am 13 Apr. 2016
I am trying to add values to a matrix of 2 dimensions. The rows of toothCount are the values that holds the number of times a specific condition is met. The columns of toothCount are a reference to the offset in which the conidtion is true. I need this to build an array. Here is the working 1D version:
toothCount((find(tmpVec==1)))=toothCount((find(tmpVec==1)))+ones(size(find(tmpVec==1)));
%tmpVec is the condition that must be met in order for that row of toothCount to increment.
How do I go about turning this into a growing 2 dimensional array? This call is within a double for loop, so I want the parent for loop to increment the column and the condition & child for loop take care of the values of the rows (as it is doing in the above 1D version). Is there an easy way to do this?

Akzeptierte Antwort

Midimistro
Midimistro am 13 Apr. 2016
I found the answer. Here's a simplified, better explanation of what I am doing:
A={1.2,1.3,1.6}
B={1.15,1.2,1.22,1.23,1.28,1.29,1.35,1.37,1.59,1.60}
Asize=numel(A);
Bsize=numel(B);
Increment=A;
IncrementInstances= int8(abs(testOffsetStart-testOffsetFinish)/.01);
toothCount = zeros (numel(A),IncrementInstances);
%numel(A) defines number of rows, IncrementInstances defines # of columns.
%toothCount needs to be preallocated beforehand, otherwise you will get a index out of bounds error
toothColumn = 1; % the starting column value of the toothCount matrix
for i = testOffsetStart:.01:testOffsetFinish
{
Increment=A+i;
for j=1:numel(B)
{
if(%Conditional here. Not included for certain reasons)
{
clear tmpVec; %release values from last evaluation;
tempVec=(%insert same conditional here);
toothCount((find(tmpVec==1)), toothColumn)=toothCount((find(tmpVec==1)), toothColumn)+ones(size(find(tmpVec==1)));
toothColumn=toothColumn+1;
%do more stuff here if desired
}
}
}
The above matrix will return a count at each of the given indexes in which A happened to pass the conditional for each offset. If anyone needs a better explanation, please let me know.
In other words, it creates a new column consisting of the 1D toothCount vector, except each column of the toothCount matrix refers to a specific offset.
  1 Kommentar
Guillaume
Guillaume am 13 Apr. 2016
There's no concatenation whatsoever in your answer.
As stated in my answer, the find and the ones expression are completely unnecessary and just slow down the code:
toothCount(tmpVec == 1, toothColumn) = toothCount(tmpVec == 1, toothColumn) + 1;
The clear tmpVec is also completely unnecessary.
Your code is also going to fail if the difference between testOffsetFinish and testOffsetStart is greater than 2.55. You should use proper rounding functions such as floor, round, fix or ceil rather than converting to 8-bit integer with int8.
However, there's still the risk that your calculation of IncrementInstances is off by one compared to the actual number of steps in the for loop due to rounding error (particularly as .01 can't be represented exactly as double). Safer would be to actually generate the iterating vector for the loop and count the number of elements:
Offsets = testOffsetStart : 0.01 : testOffsetFinish;
IncrementInstances = numel(Offsets);
%...
for i = Offsets
%...

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Guillaume
Guillaume am 12 Apr. 2016
Bearbeitet: Guillaume am 12 Apr. 2016
A simpler expression for your 1D case would be:
toothCount(tempVec == 1) = toothCount(tempVec == 1) + 1;
Assuming, that tempVec (now tempMat or better a name that actually mean something) is now a matrix the same size and dimension as toothCount the above will work for any dimension.
If not, you need to give more details on what tempVec is.
edit: I'm not sure why you've got a 'concatenation' tag since your example does not involve any concatenation.
  2 Kommentare
Midimistro
Midimistro am 12 Apr. 2016
tmpVec returns the result of a conditional (which is 2 vectors of different size using what's called a combing algorithm), returning a 0 or 1, for a specific set of indexes. ToothCount then adds one to each instance where tempVec is 1.
However, this is not what I am looking for. I am looking to create a 2D array that does the same thing as what I have specified, but adds it as a column of a growing matrix (aka why I added the concatenation tag).
Guillaume
Guillaume am 13 Apr. 2016
Bearbeitet: Guillaume am 13 Apr. 2016
"but adds it". What is it?
Can you give a concrete example of what you want?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by