'Logical' function to read 2 variables and output to a new variable

6 visualizaciones (últimos 30 días)
10B
10B el 25 de Sept. de 2015
Editada: dpb el 2 de Oct. de 2015
Hello Community,
I am having a novice moment here - difficulty in organising my function to do what I want. In short, I am trying to:
1. Read the data in 'a', AND
2. Read the data in 'b'
3. When they agree with the terms (a basic logical test)
4. A numerical count is recorded in the appropriate row/column of 'Imloc'
Example, if a(:,1) & b(:,1) == '1', then in IMloc(1,1) a count is recorded (or nothing if not). If a(:,2) & b(:,1) == '1' a count would be recorded at Imloc(1,2) etc.
Here is what code I have:
function [ Imloc ] = IMLocation( a, b )
% Preallocate Imloc matrix with zeros
Imloc = zeros(1,11);
% Set counter
numout = 0;
% Create storage for output vector
outvec = [];
% Run for the length of
for k=1:length(a,b)
if a(k)==1 && b(k)==1
numout = (numout + 1), 'Imloc',(1:1))
Imloc(outvec(numout))
elseif a(k)==2 && b(k)==1
numout = (numout + 1) 'Imloc',1:2))
Imloc(outvec(numout))
elseif a(k)==3 && b(k)==1
numout = (numout + 1) 'Imloc',1:3))
Imloc(outvec(numout))
% etc. etc. etc.
elseif a(k)==11 && b(k)==1
numout = (numout + 1) 'Imloc',1:11))
Imloc(outvec(numout))
else
end
end
So, can anyone make a suggestion as to what is going wrong as I am not doing very well with this!
Regards,
10B.
  3 comentarios
10B
10B el 26 de Sept. de 2015
Hello again dpb,
Thanks for taking time to look at this. 'a' and 'b' are both (137,1) - I know their size has to match, and they look like this:
a b
1 0
2 0
1 1
2 1
1 1
2 1
3 0
4 0
5 1
6 1
1 0
2 1
% 'a' continues up to 11, and 'b' is either 1 or 0
So, when the conditions are met e.g. a=1 & b=1, then a count will be recorded in the new variable 'Imloc' at (2,1) (location (1,1) would be for a title eg '1m'). This process should iterate through all the data with the query changing the 'a' value to the next integer eg '2', which would result in the counter scoring in 'Imloc' at (2,2) under the title '2m', and so on until it has gone through all the 'a' options up to 11. I really hope that makes sense! Expected output for Imloc should look something like this:
1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 11m
3 5 6 9 8 6 6 5 7 9 6
Please let me know if this answers your question properly. I'll keep an eye out for your response.
Regards,
10B.
10B
10B el 29 de Sept. de 2015
Sorry to send a reminder dpb - but did my comment answer your question for this?
Kind regards,
10B.

Iniciar sesión para comentar.

Respuesta aceptada

dpb
dpb el 29 de Sept. de 2015
Sorry, I didn't see the previous response. Your dataset isn't complete, but presuming it is, then
d=a(b==1); % only the 1's count...
u=unique(d); % the unique values that are in a
n=hist(d,u); % count for each possible bin
Your header row or whatever is simply the vector u with the 'm' appended if want (altho you'll then have to use a cell array to hold both datatypes together, of course)...
>> cellstr(num2str(u,'%dm')).'
ans =
'1m' '2m' '5m' '6m'
>>
for the above (partial) dataset...
  2 comentarios
10B
10B el 29 de Sept. de 2015
Excellent dpb!
Your solution is so much less complicated then my approach - definitely less is more. Thanks very much.
dpb
dpb el 29 de Sept. de 2015
Editada: dpb el 2 de Oct. de 2015
You're welcome, of course. Was why was checking for sure on what the actual objective was, first...I thought this was likely the question initially, but wasn't positive so figured may as well solve the question asked...
ADDENDUM
Of course, if the data are complete in the real case, accumarray works wonders here...
>> accumarray(a,b).'
ans =
2 3 0 0 1 1
>>
See the doc for details. This relies on the b vector being [0,1] so the sum is therefore identical to the count of nonzero elements.
As you see, this gives entries in the final array for each element at the index position where as the unique|hist solution only returns those actually found.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by