how do I take the matrix A and a vector with the indexes of the rows to be considered, and takes out a vector of the mean values?

조회 수: 2 (최근 30일)
I have to compute a function with the following instructions:
The matrix A is a random matrix mxm, and then I got compute the mean value of the odd columns. Finally I have to compute a function that takes in the matrix A and a vector with the indexes of the rows to be considered(index of whatever row), and takes out a vector of the mean value.
I tried this function, but it doesn't work.
function A(meanValue) = HW3Part3Function
A(meanValue) = mean((A(1:2:end,:),2))
end
Thanks in advanced!
  댓글 수: 3
JP Retaw
JP Retaw 2021년 3월 2일
I tried with this, but it doesn't work
function A(meanValue) = HW3Part3Function
A(meanValue) = mean((A(1:2:end,:),2))
end

댓글을 달려면 로그인하십시오.

채택된 답변

Steven Lord
Steven Lord 2021년 3월 2일
function A(meanValue) = HW3Part3Function
A(meanValue) = mean((A(1:2:end,:),2))
When you define your function, the function declaration line should include the names of the variables into which the input arguments will be stored and the names of the variables to be returned to the caller. It cannot contain expressions like A(meanvalue).
When you call your function, specify the exact values on which you want your function to operate. The call can contain expressions.
This is an incorrect way to define the addme function:
function z = addme(2, 3)
z = 2+3;
end
This is a correct way to define the addme function:
function z = addme(x, y)
z = x + y;
end
and these are correct ways to call the addme function.
theOutput(17) = addme(2, 3)
theOutput(2) = addme(pi^2, sind(45))
See this documentation page for more information on defining functions.
In addition, nowhere in your code prior to using the A and meanValue variables do you use them, so even if you corrected the function declaration line your function still would not work.
I second Cris LaPierre's suggestion that you go through the MATLAB Onramp as I believe it will teach you how to write functions in MATLAB.

추가 답변 (1개)

Mathieu NOE
Mathieu NOE 2021년 3월 2일
hello
title and question are not consistent: odd rows or odd columns ?
result for odd columns :
out = mean(A(:,1:2:end),1)
result for odd rows
out = mean(A(1:2:end,:),2)
  댓글 수: 2
JP Retaw
JP Retaw 2021년 3월 2일
Thank you a lot and sorry for the incosistency. But How would I do in case for the matrix A and a vector (whatever) with the indexes of the vector to be sonsidered?
thanks in advanced!
Mathieu NOE
Mathieu NOE 2021년 3월 2일
hello
sorry, I don't understand - what vector would be used in conjunction with this matrix ?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by