Ones function printing out
조회 수: 6 (최근 30일)
이전 댓글 표시
Can anyone tell me why my ones function is causeing my code to print even though it has a semi colon? I commented everything else out to narrow it down to the ones function. I already reinstalled matlab but nothing seems to be working
getX1();
function X1 = getX1()
clc;
close;
clear;
load('mnist.mat', 'images'); % Load 'images' variable from 'mnist.mat'
num_images = size(images, 1);
image_size = 26 * 26; % Adjusted image size
X1 = ones(num_images, image_size + 1); % Initialize X matrix with bias column
for i = 1:num_images
%Crop 1-pixel border
image_cropped = images(i, 2:end-1, 2:end-1);
%Flatten image, normalize, and append 1
image_flat = double(reshape(image_cropped, 1, [])) / 255;
image_with_bias = [image_flat, 1];
% Update X matrix
X1(i, :) = image_with_bias;
end
% size of X matrix
fprintf('Size of X matrix: %d rows x %d columns\n', size(X1, 1), size(X1, 2));
end
댓글 수: 11
VBBV
2024년 2월 25일
It seems you are running the script file getX1.m from a wierd location. Change its location to a different folder and run again
The path from the screenshot shows something like this
C:\users\zach1\OneDrive\...\Project1\mnist.mat\getX1.m
mnist.mat >> change this to a different name without a dot and check whether you still get that issue
Walter Roberson
2024년 2월 25일
That will not make any difference. mnist.mat is a valid directory name.
답변 (2개)
Walter Roberson
2024년 2월 24일
The ones is the output of your function. You are running the function by pressing the green Run button, which executes the function which returns X1 which then gets printed out automatically.
Star Strider
2024년 2월 24일
The fprintf call will execute and print even if there is a semicolon at the end of that call.
X1 = rand(3,5);
fprintf('Size of X matrix: %d rows x %d columns\n', size(X1, 1), size(X1, 2));
Are you seeing that or something else?
.
댓글 수: 6
Star Strider
2024년 2월 24일
It’s coming from somewhere else in your code. I doubt if anything in the recycling bin is in any way active.
I don’t know how long your code is, however in the past when I needed to isolate something like that, I inserted:
fprintf('Stopped on line %d\n', line_nr)
return
and then moved those lines to various points in my code and then looked to see if the problem recurred. (I inserted the line number manually.) That helped me find the problem.
There are a collection of debugging tools available, however they might not be helpful in tracking down a line that prints a matrix, and that’s all you want to do at the moment.
참고 항목
카테고리
Help Center 및 File Exchange에서 Function Creation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
