using if else statements to assign a value to a variable based on the value of a specific element in a matrix

조회 수: 17 (최근 30일)
I've created a matrix and I'm trying to write a for loop and within the for loop, and I have a variable, p, that I want to change based on a specific element within a matrix that changes with every loop
Here's what I have:
if M(j,k)>0
p(j,k)=0.9;
elseif M(j,k)<0
p(j,k)=0.1;
elseif M(j,k)==0
p(j,k)=0.5;
end
However, when I try to run it, it just creates p as equalling 0.5 and I get an "index in position 2 exceeds array bounds"
Here's an example of how I'm trying to run it
p(y(j),z1(k))*(n1*Fd(xp(i),zp(j),y(k),tt+1)
So what I'm trying to say here is that if we have value j, modified by y, and value k, modified by z1, looking at that placement in matrix M, if its above 0, then p = 0.9

채택된 답변

Hassaan
Hassaan 2024년 4월 19일
편집: Hassaan 2024년 4월 19일
An initial idea:
% Example matrix M
M = randn(10,10); % A 10x10 matrix with random values
% Initialize matrix p with the same size as M
p = zeros(size(M));
% Assuming y and z1 are vectors that map to indices in M
y = 1:length(M); % Example index mapping, adjust as per your requirements
z1 = 1:length(M); % Example index mapping, adjust as per your requirements
% Loop over the indices
for j = 1:length(y)
for k = 1:length(z1)
if M(y(j), z1(k)) > 0
p(y(j), z1(k)) = 0.9;
elseif M(y(j), z1(k)) < 0
p(y(j), z1(k)) = 0.1;
else
p(y(j), z1(k)) = 0.5;
end
end
end
disp(p)
0.9000 0.1000 0.9000 0.1000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.1000 0.1000 0.1000 0.1000 0.1000 0.9000 0.1000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.1000 0.9000 0.1000 0.9000 0.1000 0.1000 0.1000 0.9000 0.9000 0.1000 0.9000 0.9000 0.1000 0.1000 0.1000 0.9000 0.9000 0.9000 0.1000 0.9000 0.1000 0.1000 0.9000 0.9000 0.1000 0.9000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.9000 0.1000 0.1000 0.1000 0.1000 0.1000 0.9000 0.9000 0.1000 0.1000 0.1000 0.1000 0.9000 0.9000 0.1000 0.1000 0.9000 0.9000 0.1000 0.1000 0.1000 0.1000 0.9000 0.1000 0.1000 0.9000 0.9000 0.9000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.9000 0.1000 0.1000 0.1000 0.9000 0.9000 0.9000 0.9000
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
  댓글 수: 3
Hassaan
Hassaan 2024년 4월 19일
Yes. Adjust as per your needs.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by