Hi, I have the program below which when run gives extracted matrices with corresponding means. My problem is to tell the program to ouput all the means inside onesinlge matrix. Can someone assist me with this kindly. My input for A and B were A=[1 1 1 1 2 2 2 2 3 3 3 3]'; B=[3 4 6 7 8 9 0 12 4 5 6 8]';
%when this code is run and you input A and B which were (A=[1 1 1 1 2 2 2 2 3 3 3 3]'; B=[3 4 6 7 8 9 0 12 4 5 6 8]';)
A=input('input sample matrix\n');
B=input('enter the second matrix\n');
C=[A B];
for i=1:1:3
selectedRowsLogicalIndices = A == i;
subsetBdata = B(selectedRowsLogicalIndices)
mean(subsetBdata)
end
Below are the reulst that i got;
subsetBdata =
3
4
6
7
ans =
5
subsetBdata =
8
9
0
12
ans =
7.2500
subsetBdata =
4
5
6
8
ans =
5.7500
>>
I want all the means to be in matrix form like below;
ouput I want is below;
[5 7.25 5.75]'
or a matrix of all the subdatasets

4 comentarios

Rik
Rik el 22 de En. de 2023
Why can't you store the results in a vector using your loop variable as the index?
okoth ochola
okoth ochola el 22 de En. de 2023
@Rik if you dont mind, i request you just demostrate with an example. Am in the process of learning matlab, therefore I may not much experienced. Kindly.
Walter Roberson
Walter Roberson el 22 de En. de 2023
output(i) = value
okoth ochola
okoth ochola el 22 de En. de 2023
@Rik thank you, this tip just gave me a clue and it worked just right. Thank you so much.

Iniciar sesión para comentar.

 Respuesta aceptada

Sargondjani
Sargondjani el 22 de En. de 2023

0 votos

D(i,1) = mean(subsetBdata)

2 comentarios

okoth ochola
okoth ochola el 22 de En. de 2023
What if I wanted to do the same with the vectors, kindly give some clue @Sargondjani
output(i, :) = rowvector
output(:, i) = columnvector

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Productos

Versión

R2016a

Etiquetas

Preguntada:

el 22 de En. de 2023

Comentada:

el 22 de En. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by